[R] Find and replace all the elements in a data frame

2011-02-17 Thread Josh B
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

Re: [R] Find and replace all the elements in a data frame

2011-02-17 Thread Sarah Goslee
Josh, you've made it far too complicated. Here's one simpler way (note that I changed your read.table statement to make the values NOT factors, since I wouldn't think you want that). x - read.table(textConnection(locus1 locus2 locus3 + A T C + A T NA + T C C + A T G), header = TRUE, as.is=TRUE)

Re: [R] Find and replace all the elements in a data frame

2011-02-17 Thread Henrique Dallazuanna
Try this: xNew - as.data.frame(mapply(paste, x, x, sep = /)) xNew[is.na(x)] - NA xNew On Thu, Feb 17, 2011 at 2:54 PM, Josh B josh...@yahoo.com wrote: Hi all, I'm having a problem once again, trying to do something very simple. Consider the following data frame: x -

Re: [R] Find and replace all the elements in a data frame

2011-02-17 Thread baptiste auguie
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,

Re: [R] Find and replace all the elements in a data frame

2011-02-17 Thread Gong-Yi Liao
You may write as this: for (i in 1:nrow(x)){ for (j in 1:ncol(x)){ if (!is.na(x[i, j])) { 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{