r - Turning for loop to apply -


i have tried understand how apply works having hard time understaning how use it. have df frame called sso9 172*92. 1:s or 0:s elements in it.

col1 col2 col3 ... 1      0    1 0      1    1 0      0    1 . . 

i want take each permutation there when choosing 3 columns , take rowsum. meaning first first, second , third column , take row sum , save in new df, first secound , 4 , on. meaning reslut in 92 choose 3 = 125580 column.

ds=data.frame(rowsums(sso9[,c(1,2,3)]))  for(i in 1:90){   for(j in (i+1):91){      for(k in (j+1):92){     temp<-data.frame(rowsums(sso9[,c(i,j,k)]))     colnames(temp)<-(paste(colnames(sso9[i]),colnames(sso9[c(j)]),paste(colnames(sso9[k]),sep=",")))     ds=cbind(ds,temp)      rm(temp)     }   } } ds$rowsums.sso9...c.1..2..3...<-null 

this code work kind of slow, try use apply insted of 3 loops, have no idea how make use of apply, when want permutations. can see create df ds taking rowsum of first 3 columns deleting dimentions right, there better way that.

the first thing suggest storing input matrix, since has 1s , 0s, , these kinds of numerical operations more performant on matrices data.frames.

here's how can accomplished using combn(), apply(), , rowsums():

set.seed(1); nr <- 172; nc <- 92; sso9 <- matrix(sample(0:1,nr*nc,replace=t),nr,dimnames=list(null,paste0('col',1:nc))); head(sso9[,1:10]); ##      col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 ## [1,]    0    1    1    1    0    0    0    1    1     0 ## [2,]    0    0    0    1    1    0    1    1    1     1 ## [3,]    1    0    0    0    0    1    1    1    0     0 ## [4,]    1    1    0    1    1    0    0    1    1     0 ## [5,]    0    1    0    1    1    0    0    0    1     1 ## [6,]    1    1    1    1    1    0    1    1    1     0 system.time({     comb <- combn(ncol(sso9),3);     res <- apply(comb,2,function(cis) rowsums(sso9[,cis]));     colnames(res) <- apply(comb,2,function(cis) paste(colnames(sso9)[cis],collapse=',')); }); ##    user  system elapsed ##   3.422   0.203   3.626 dim(res); ## [1]    172 125580 head(res[,1:10]); ##      col1,col2,col3 col1,col2,col4 col1,col2,col5 col1,col2,col6 col1,col2,col7 col1,col2,col8 col1,col2,col9 col1,col2,col10 col1,col2,col11 col1,col2,col12 ## [1,]              2              2              1              1              1              2              2               1               2               2 ## [2,]              0              1              1              0              1              1              1               1               0               0 ## [3,]              1              1              1              2              2              2              1               1               2               2 ## [4,]              2              3              3              2              2              3              3               2               3               3 ## [5,]              1              2              2              1              1              1              2               2               1               1 ## [6,]              3              3              3              2              3              3              3               2               2               2 

also note these combinations, not permutations, since order not matter.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -