[R] optimized R-selection and R-replacement inside a matrix need, strings coerced to factors
Christine SINOQUET
christine.sinoquet at univ-nantes.fr
Sun Feb 7 15:13:11 CET 2010
Hello,
I need to modify some huge arrays (2000 individuals x 50 000
variables).
To format the data, I think I should benefit from optimized R-selection
and R-replacement inside a matrix and prohibite a naive use of loops.
Thank you in advance for providing information about the following problem :
file A :
2 000 individuals in rows
50 000 columns corresponding to 50 000 variables : each value belongs to
{0, 1, 2}
file B :
50 000 variables in rows
1st column : character (A,C,G,T) corresponding to code 0
2nd colomn : character corresponding to code 1
convention:
if A[,j]=0, one wants to replace 0 with character in B[j,1] twice
if A[,j]=1, one wants to replace 1 with character in B[j,1] and
character in B[j,2]
if A[,j]=2, one wants to replace 2 with character in B[j,2] and
character in B[j,2]
C <- matrix(0,2000,0) # initialization to void matrix
for(j in 1:2000){
c <- A[,j]
zeros <- which(c==0);
ones <- which(c==1);
twos <- which(c==2);
rm(c)
c1 <- matrix("Z",2000)
c2 <- matrix("Z",2000)
c1[zeros] <- B$V1[j]; c2[zeros] <-B$V1[j]
c1[ones] <- B$V1[j]; c2[ones] <-B$V2[j]
c1[twos] <- B$V2[j]; c2[twos] <-B$V2[j]
C <- cbind(C, cbind(c1,c2))
}
I do think some more elaborated solution might exist.
Thanks in advance for your help.
More information about the R-help
mailing list