i have matrix called dataset contains high throughput data below :
v1 v2 v3 2 3 3 b 4 2 7 c 3 1 4 what want find combinations of row products matrix in form of list, shown below (i'll call comb):
v1 v2 v3 a 4 9 9 b 8 6 21 c 6 3 12 b b 16 4 49 b c 12 2 28 c c 9 1 16 what have far following:
combs <- combn(seq_len(nrow(dataset)), 2) comb <- dataset[combs[1,], ] * dataset[combs[2,], ] rownames(comb) <- apply(combn(rownames(dataset), 2), 2, paste, collapse = " ") unfortunately, main problem in using script don't products of rows multiplied themselves. using above script, following matrix:
v1 v2 v3 b 8 6 21 c 6 3 12 b c 12 2 28 so wondering if possible modify code have in such way multiply values in same row together? or there might more efficient? when tried script on high throughput dataset (which large), seemed take several seconds output list table 1000 rows, if knows of way task might faster, i'd love hear thoughts.
thanks help!
here minimal adaptation of implementation. add combn result values want, , pretty use same logic:
r.seq <- seq_len(nrow(dataset)) combs <- matrix(c(combn(r.seq, 2), rep(r.seq, each=2)), nrow=2) # notice how add values here comb <- dataset[combs[1,], ] * dataset[combs[2,], ] rownames(comb) <- apply(matrix(rownames(dataset)[c(combs)], nrow=2), 2, paste, collapse=" ") produces:
v1 v2 v3 b 8 6 21 c 6 3 12 b c 12 2 28 a 4 9 9 b b 16 4 49 c c 9 1 16 you can sort rowname too. 1 advantage on expand grid calculates combinations want.
Comments
Post a Comment