[R] help on replacing values
darteta001 at ikasle.ehu.es
darteta001 at ikasle.ehu.es
Fri Sep 7 16:02:24 CEST 2007
Dear List,
I have a newbie question. I have read in a data.frame as follows:
> data = read.table("table.txt", header = T)
> data
X1 X2 X3 X4
A AB AC AB AC
B AB AC AA AB
C AA AB AA AB
D AA AB AB AC
E AB AA AA AB
F AB AA AB AC
B AB AC AB AA
I would like to replace AA values by BB in column X2. I have tried
using replace() with no success, although I am not sure this is the
right function. This is the code I have used:
data$X2 <- replace(data$X2, data$X2 =="AA","BB")
Warning message:
invalid factor level, NAs generated in: `[<-.factor`(`*tmp*`, list,
value = "BB")
What is wrong with the code? How can I get this done? how about
changing AA values by BB in all 4 columns simultaneously? Actually
this is a small example dataframe, the real one would have about 1000
columns.
Extendind this, I found a similar thread dated July 2006 that used
replace() on iris dataset, but I have tried reproducing it obtaining
same warning message
iris$Species <- replace(iris$Species, iris$Species
== "setosa","NewName")
Warning message:
invalid factor level, NAs generated in: `[<-.factor`(`*tmp*`, list,
value = "NewName")
Thanks in advance your help,
David
More information about the R-help
mailing list