[R] Changing some values within a variable

Toni Pitcher toni.pitcher at otago.ac.nz
Fri Jul 23 05:36:28 CEST 2010


All sorted


Thanks very much for your help Peter and Jannis.

Toni







Peter Alspach wrote:
> Tena koe Toni
>
> Assume your data is in a data.frame called toniData (naming a data.frame data is not a good idea as data is a function in R - see ?data), and that Group is of class character (try str(toniData)) then:
>
> toniData[toniData$Group %in% 'A','Group'] <- 'C'
>
> will work.  But from your message below I suspect Group is of class factor.  So you either need to change its class:
>
> toniData$Group <- as.character(toniData$Group)
>
> or rename the level names.  For example:
>
> set.seed(0)
> toniG <- factor(LETTERS[sample(1:2, size=10, replace=TRUE)])
> toniG
>  [1] B A A B B A B B B B
> Levels: A B
> levels(toniG)
> [1] "A" "B"
> levels(toniG) <- c('C','B')
> toniG
>  [1] B C C B B C B B B B
> Levels: C B
>
> Regarding your second question, see sub() (or gsub()) - again paying attention to whether or not you are dealing with factor or character variables.
>
> HTH ....
>
> Peter Alspach
>
>   
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Toni Pitcher
>> Sent: Friday, 23 July 2010 9:44 a.m.
>> To: r-help at r-project.org
>> Subject: [R] Changing some values within a variable
>>
>> Hi
>>
>> I'm new to R and would like some help with a couple of problems. I
>> suspect the solutions are quite simple.
>>
>> I have a data.frame (data) with 40 variables and 5238 observations
>> created from ~150 text files using read.table.
>>
>> I would like to change some of the entries within two different
>> columns.
>>
>> Firstly, in the Group column I have groups A and B, I would like to
>> select all As and change to C. I have tried the following;
>>
>> data$Group = ifelse(data$Group == 'A', "C", data$Group)
>> data$Group <- as.factor(data$Group)
>>
>> This works to some extent, all As have been changes to Cs, but the Bs
>> have also been changed, in this case to "2", instead of remaining as
>> their original values. How do I get the Bs to stay the same?
>>
>>
>> Second problems is similar;
>>
>> I need to change some of the Subject IDs. The required format is 3
>> numbers followed by 3 letters (999LLL). Some of the IDs have format
>> 999LLL-LL, Thus I need to delete the -LL from the IDs.
>> How do I generically specify "find 999LLL-LL and replace with 999LLL"?
>>
>>
>> Many thanks in advance
>>
>> Toni
>>
>>
>>
>>
>> --
>> Toni Pitcher PhD
>> Dept. of Medicine
>> University of Otago, Christchurch
>>
>> Van der Veer Institute for Parkinson's and Brain Research
>> 66 Stewart St
>> Christchurch 8011
>> New Zealand
>>
>> Phone: +64 3 378 6075 (internal extn 86075)
>> Email: toni.pitcher at otago.ac.nz
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>     


-- 
Toni Pitcher PhD
Dept. of Medicine
University of Otago, Christchurch

Van der Veer Institute for Parkinson's and Brain Research
66 Stewart St 
Christchurch 8011
New Zealand

Phone: +64 3 378 6075 (internal extn 86075)
Email: toni.pitcher at otago.ac.nz



More information about the R-help mailing list