Re: [R] manupulating a data frame column

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Fri 08 Jul 2005 - 07:23:00 EST

Steven T. wrote:

> Could someone tell me how to fix the following error? It looks like
> that the reason is that df$x is of the class "factor". Thanks!

You are right.

Either don't make it a factor, if you don't want, or try something like the following in order to add the relevant levels to df$x1:

x1<-LETTERS[1:8]; x2<-letters[1:8]; x1[2]<-NA; x1[4]<-NA df<-data.frame(x1=x1, x2=x2)

levels(df$x1) <- c(levels(df$x1), levels(df$x2))

idx<-which(is.na(df$x1))
df[idx,1]<-df[idx,2]

Uwe Ligges

>

>>x1<-LETTERS[1:8]; x2<-letters[1:8]; x1[2]<-NA; x1[4]<-NA;
>>df<-data.frame(x1=x1, x2=x2)
>>idx<-which(is.na(df$x1))
>>df[idx,1]<-df[idx,2]

>
> Warning message:
> invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, iseq,
> value = c(2, 4))
>
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jul 08 07:27:23 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:22 EST