[R] Find and replace all the elements in a data frame
baptiste auguie
baptiste.auguie at googlemail.com
Thu Feb 17 18:29:33 CET 2011
Hi,
You could use car::recode to change the levels of the factors,
library(car)
transform(x, locus1 = recode(locus1, "'A' = 'A/A' ; else = 'T/T'"),
locus2 = recode(locus2, "'T'='T/T' ; 'C' = 'C/C'"),
locus3 = recode(locus3, "'C'='C/C' ; 'G' = 'G/G'"))
HTH,
baptiste
On 17 February 2011 17:54, Josh B <joshb41 at yahoo.com> wrote:
> Hi all,
>
> I'm having a problem once again, trying to do something very simple. Consider
> the following data frame:
>
> x <- read.table(textConnection("locus1 locus2 locus3
> A T C
> A T NA
> T C C
> A T G"), header = TRUE)
> closeAllConnections()
>
> I am trying to make a new data frame, replacing "A" with "A/A", "T" with "T/T",
> "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" (missing
> data) in the data frame, which should be carried over to the new data frame.
>
> Here is what I am trying, which fails miserably:
>
> x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x)))
>
> for (i in 1:nrow(x)){
> for (j in 1:ncol(x)){
> if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{
> if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{
> if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{
> if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- NA}
> }
> }
> }
> }
> }
>
> I get the following error message:
> Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed
>
> So what am I doing wrong? If you can provide me with specific code that fixes
> the problem and gets the job done, that would be the most useful.
>
>
> Thanks very much in advance for your help!
>
> Sincerely,
> -----------------------------------
> Josh Banta, Ph.D
> Center for Genomics and Systems Biology
> New York University
> 100 Washington Square East
> New York, NY 10003
> Tel: (212) 998-8465
> http://plantevolutionaryecology.org
>
>
>
> [[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