Re: [R] help on replacing values
Your columns are factors, not character strings. Use as.is = TRUE as an argument to read.table. Also its a bit dangerous to use T although not wrong. Its safer to use TRUE. On 9/7/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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 __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.
[R] help on replacing values
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 __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] help on replacing values
Thanks a lot Gabor, that was very helpful. All sorted now! Best David Your columns are factors, not character strings. Use as.is = TRUE as an argument to read.table. Also its a bit dangerous to use T although not wrong. Its safer to use TRUE. On 9/7/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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 __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.