replace two values of a row with one in R dataframe -
it may simple task having trouble in doing this. sorry if silly question asking here after many attempts.
i have 399 files named prediction1, prediction2, ..... on, , each file containing 2 values like
in file predition1
0.1234 -1.2345
in file prediction2
-1.5443 0.34436
..... on
these files imported in r using
filelist = list.files(pattern = "pred*") myfiles = lapply(filelist, read.table)
and list object "myfiles" converted dataframe using:
myfiles2 = as.data.frame(myfiles)
now myfiles2 contains:
> myfiles2[1] v1 1 -1 2 -1
up 399 times.
now have apply conditions if(myfiles2[1][1,]>0 & myfiles2[1][2,]<0)
thenreplace values numeric "1001" and
if(myfiles2[1][1,]<0 & myfiles2[1][2,]>0)` replace values numeric "0110"..
can suggest me right way this, able print values in r console not getting values in object can merge make 1 matrix.
i hope discussed problem better previous.
for(i in 1:399) { ab = null; if(myfiles2[i][1,]>0 & myfiles2[i][2,]<0) { myfiles2[i] = "1001" } if(myfiles2[i][1,]>0 & myfiles2[i][2,])>0) { myfiles2[i] = "1010" } if(myfiles2[i][1,]<0 & myfiles2[i][2,]<0) { myfiles2[i] = "0101" } else { myfiles2[i] = "0110" } ab = append(ab, myfiles[i]) }
this code using , getting last replaced value, appending problem think
try
d1 <- cbind(expand.grid(rep(list(c(-1,1)),2)), var3=c('0101', '1001', '0110', '1010'),stringsasfactors=false) myfiles2[] <- lapply(myfiles2, function(x) { x[] <-d1[!colsums(t(d1[,-3])!=sign(x)),3] x}) myfiles2[1:3] # v1 v2 v3 #1 0110 1010 0110 #2 0110 1010 0110
data
set.seed(22) myfiles2 <- as.data.frame(matrix(rnorm(100), nrow=2)) myfiles2[1:3] # v1 v2 v3 #1 -0.5121391 1.0078262 -0.2089594 #2 2.4851837 0.2928146 1.8580924
Comments
Post a Comment