[R] modifying factors in a data frame

Peter Alspach Peter.Alspach at plantandfood.co.nz
Thu Aug 19 07:50:33 CEST 2010


Tena koe Abhi

The easiest way is probably

data1$sample <- as.numeric(data1$sample)

If you want it to remain as a factor then

data1$sample <- as.factor(as.numeric(data1$sample))

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Abhishek Pratap
> Sent: Thursday, 19 August 2010 5:41 p.m.
> To: r-help at r-project.org
> Subject: [R] modifying factors in a data frame
> 
> Hi All
> 
> I have a data frame in which on the columns is a factor with following
> levels
> 
> Levels: M006 M0114 M0379 M0432 M0823 M1012 M1096 M1107 SW393 SW708
> 
> I want to change all the M006 to "1", M0114 to "2" and so on.  What I
> am
> trying is not working.
> 
> data1$sample[data1$sample =='M006'] <- "1"
> Warning message:
> In `[<-.factor`(`*tmp*`, data1$sample == "M006", value = "1") :
>   invalid factor level, NAs generated
> 
> 
> Any slick way to do this ?
> 
> Thanks!
> -Abhi
> 
> 	[[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