[R] a question about replacing a column that is factor

Paul Emberson email at calidasoft.co.uk
Sat Jul 25 17:17:53 CEST 2009


Hi,

You can add extra levels using levels().

e.g.

 > test <- data.frame(x=c(1,2,4,4), y=c("A", 1, 2, "A"))
 > levels(test$y) <- c(levels(test$y), 'B')
 > levels(test$y)
[1] "1" "2" "A" "B"
 > test[test$x==4,]$y<-'B'
 > test
   x y
1 1 A
2 2 1
3 4 B
4 4 B

Regards,

Paul

jlfmssm wrote:
> Sorry, I didn't get it.
> I made a simple example to explain what I want
> 
> 
>> x=c(1,2,4,4)
>> y=c("A",1,2,"A")
>> test<-as.data.frame(cbind(x,y))
>> test
>   x y
> 1 1 A
> 2 2 1
> 3 4 2
> 4 4 A
>> test[test$x==4,]$y<-'B'
> Warning message:
> In `[<-.factor`(`*tmp*`, iseq, value = c("B", "B")) :
>   invalid factor level, NAs generated
>> test
>   x    y
> 1 1    A
> 2 2    1
> 3 4 <NA>
> 4 4 <NA>
> 
> actually, what I want is
> 
>> test
>   x    y
> 1 1    A
> 2 2    1
> 3 4    B
> 4 4    B
> 
> jlm
> 
> thanks,
> 
> 
> On Sat, Jul 25, 2009 at 12:11 AM, milton ruser <milton.ruser at gmail.com>wrote:
> 
>> May be you want something like:
>>
>> FDLFDPdata0000[FDLFDPdata0000$ID==69912008,]$tempcalved2<-'2008-03-08'
>> FDLFDPdata0000$tempcalved2<-factor(FDLFDPdata0000$tempcalved2)
>> ## the factor above just update the levels
>>
>> Case this is what you want, you can also change
>>
>> $ID==69912008
>> $ID %in% c(69912008,....)
>>
>> Good luck,
>>
>> milton
>>
>>
>>
>>
>> On Fri, Jul 24, 2009 at 10:33 PM, jlfmssm <jlfmssm at gmail.com> wrote:
>>
>>> Hi, everyone,
>>>
>>> I want to use a new value to replace a column that is a factor. However
>>> this
>>> column is replaced by <NA>
>>> , which is not what I want. I know this is because the new value is not in
>>> the levels of that column, but
>>> I don't know how to fix it.
>>> Can someone help me to figure this out?
>>>
>>> The following is what I did
>>>
>>> thanks
>>>
>>> jlm
>>>
>>>
>>>  > head(FDLFDPdata0000[FDLFDPdata0000$ID==69912008,])
>>>            ID Target Actual       Date     Time tempcalved2 tempLact2  DIM
>>>          69912008     50    0.0 2008-01-16 14:42:00  2009-04-14         1
>>> -454
>>>          69912008     50   51.8 2008-01-17 05:23:00  2009-04-14         1
>>> -453
>>>          69912008     50    0.0 2008-01-17 14:01:00  2009-04-14         1
>>> -453
>>>          69912008     50  -53.8 2008-01-17 16:36:00  2009-04-14         1
>>> -453
>>>          69912008      0  -26.0 2008-01-18 06:38:00  2009-04-14         1
>>> -452
>>>          69912008     50   51.6 2008-01-18 07:11:00  2009-04-14         1
>>> -452
>>>
>>>> levels(FDLFDPdata0000[FDLFDPdata0000$ID==69912008,]$tempcalved2)
>>>  [1] "2008-03-13" "2008-03-15" "2008-03-23" "2008-03-27" "2008-03-28"
>>> "2008-03-30" "2008-04-03" "2008-04-05" "2008-04-06" "2008-04-13"
>>> "2008-04-14"
>>>  [12] "2008-04-16" "2008-04-17" "2008-04-18" "2008-04-20" "2008-04-22"
>>> "2008-04-26" "2008-04-27" "2008-04-30" "2008-05-01" "2008-05-04"
>>> "2008-05-08"
>>>  [23] "2008-05-09" "2008-05-13" "2008-05-20" "2008-05-21" "2008-05-25"
>>> "2008-05-31" "2008-06-04" "2008-06-05" "2008-06-06" "2008-06-07"
>>> "2008-06-09"
>>>  [34] "2008-06-10" "2008-06-11" "2008-06-12" "2008-06-14" "2008-06-15"
>>> "2008-06-16" "2008-06-24" "2008-06-29" "2008-07-03" "2008-07-04"
>>> "2008-07-05"
>>>  [45] "2008-07-07" "2008-07-09" "2008-07-11" "2008-07-13" "2008-07-14"
>>> "2008-07-17" "2008-07-18" "2008-07-19" "2008-07-20" "2008-07-21"
>>> "2008-07-23"
>>>  [56] "2008-07-24" "2008-07-25" "2008-07-26" "2008-07-27" "2008-07-28"
>>> "2008-07-30" "2008-07-31" "2008-08-01" "2008-08-03" "2008-08-04"
>>> "2008-08-08"
>>>  [67] "2008-08-10" "2008-08-12" "2008-08-13" "2008-08-16" "2008-08-23"
>>> "2008-08-24" "2008-08-25" "2008-08-31" "2008-09-01" "2008-09-08"
>>> "2008-09-10"
>>>  [78] "2008-09-12" "2008-09-14" "2008-09-24" "2008-09-25" "2008-09-26"
>>> "2008-09-29" "2008-09-30" "2008-10-01" "2008-10-02" "2008-10-03"
>>> "2008-10-04"
>>>  [89] "2008-10-05" "2008-10-09" "2008-10-11" "2008-10-13" "2008-10-14"
>>> "2008-10-17" "2008-10-18" "2008-10-20" "2008-10-21" "2008-10-22"
>>> "2008-10-23"
>>> [100] "2008-10-25" "2008-10-27" "2008-10-28" "2008-10-29" "2008-11-04"
>>> "2008-11-07" "2008-11-08" "2008-11-09" "2008-11-10" "2008-11-12"
>>> "2008-11-14"
>>> [111] "2008-11-15" "2008-11-16" "2008-11-17" "2008-11-18" "2008-11-19"
>>> "2008-11-21" "2008-11-22" "2008-11-24" "2008-12-02" "2008-12-06"
>>> "2008-12-07"
>>> [122] "2008-12-11" "2008-12-17" "2008-12-18" "2008-12-20" "2008-12-25"
>>> "2008-12-26" "2008-12-27" "2008-12-30" "2008-12-31" "2009-01-07"
>>> "2009-01-09"
>>> [133] "2009-01-10" "2009-01-13" "2009-01-17" "2009-01-18" "2009-01-19"
>>> "2009-01-20" "2009-01-22" "2009-01-23" "2009-01-25" "2009-01-26"
>>> "2009-01-28"
>>> [144] "2009-01-29" "2009-01-31" "2009-02-01" "2009-02-02" "2009-02-04"
>>> "2009-02-05" "2009-02-07" "2009-02-08" "2009-02-09" "2009-02-11"
>>> "2009-02-12"
>>> [155] "2009-02-16" "2009-02-19" "2009-02-24" "2009-02-25" "2009-02-27"
>>> "2009-02-28" "2009-03-01" "2009-03-04" "2009-03-06" "2009-03-14"
>>> "2009-03-18"
>>> [166] "2009-03-19" "2009-03-20" "2009-03-21" "2009-03-25" "2009-03-26"
>>> "2009-03-29" "2009-03-30" "2009-04-01" "2009-04-04" "2009-04-05"
>>> "2009-04-08"
>>> [177] "2009-04-10" "2009-04-14" "2009-04-17" "2009-04-19" "2009-04-20"
>>> "2009-04-22" "2009-04-24" "2009-04-25" "2009-04-26" "2009-04-28"
>>> "2009-05-01"
>>> [188] "2009-05-02" "2009-05-03" "2009-05-04" "2009-05-05" "2009-05-06"
>>> "2009-05-07" "2009-05-08" "2009-05-11" "2009-05-15" "2009-05-17"
>>> "2009-05-18"
>>> [199] "2009-05-30" "2009-05-31" "2009-06-05" "2009-06-06" "2009-06-07"
>>> "2009-06-08" "2009-06-10" "2009-06-12" "2009-06-14" "2009-06-15"
>>> "2009-06-18"
>>> [210] "2009-06-23" "2009-06-24" "2009-06-25" "2009-06-26" "2009-06-28"
>>> "2009-06-29" "2009-07-03" "2009-07-04" "2009-07-05" "2009-07-06"
>>> "2009-07-08"
>>> [221] "2009-07-09" "2009-07-12" "2009-07-14" "2009-07-15" "2009-07-18"
>>> levels(FDLFDPdata0000[FDLFDPdata0000$ID==69912008,]$tempcalved2)[FDLFDPdata0000[FDLFDPdata0000$ID==69912008,]$tempcalved2]<-'2008-03-08'
>>> Warning message:
>>> In `[<-.factor`(`*tmp*`, iseq, value = c(1L, 1L, 1L, 1L, 1L, 1L,  :
>>>  invalid factor level, NAs generated
>>>> FDLFDPdata0000[FDLFDPdata0000$ID==69912008,]
>>>            ID Target Actual       Date     Time tempcalved2 tempLact2  DIM
>>>          69912008   50.0    0.0 2008-01-16 14:42:00        <NA>         1
>>> -454
>>>          69912008   50.0   51.8 2008-01-17 05:23:00        <NA>         1
>>> -453
>>>          69912008   50.0    0.0 2008-01-17 14:01:00        <NA>         1
>>> -453
>>>          69912008   50.0  -53.8 2008-01-17 16:36:00        <NA>         1
>>> -453
>>>          69912008    0.0  -26.0 2008-01-18 06:38:00        <NA>         1
>>> -452
>>>          69912008   50.0   51.6 2008-01-18 07:11:00        <NA>         1
>>> -452
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.




More information about the R-help mailing list