[R] compute values by condition in DF by rownames

JS Huang js.huang at protective.com
Tue Feb 17 14:14:03 CET 2015


Hi,

  I like the aggregate version.  Here is an implementation with sapply and
apply:

> data
              X2     gbm_tcga   lusc_tcga ucec_tcga_pub
1       gbm_tcga  1.000000000  0.14053719  -0.102847164
2       gbm_tcga  1.000000000  0.04413434   0.013568055
3       gbm_tcga  1.000000000 -0.20003971   0.038971817
4       gbm_tcga  1.000000000  0.14569916   0.009947045
5      lusc_tcga  0.140537191  1.00000000   0.133080708
6      lusc_tcga  0.044134345  1.00000000   0.062024713
7      lusc_tcga -0.200039712  1.00000000  -0.130239551
8      lusc_tcga  0.145699156  1.00000000   0.041796670
9  ucec_tcga_pub -0.102847164  0.13308071   1.000000000
10 ucec_tcga_pub  0.013568055  0.06202471   1.000000000
11 ucec_tcga_pub  0.038971817 -0.13023955   1.000000000
12 ucec_tcga_pub  0.009947045  0.04179667   1.000000000
> sapply(levels(data[,1]),function(x)
> apply(abs(data[as.character(data$X2)==as.character(x),-1])>0.2,2,sum))
              gbm_tcga lusc_tcga ucec_tcga_pub
gbm_tcga             4         1             0
lusc_tcga            1         4             0
ucec_tcga_pub        0         0             4



--
View this message in context: http://r.789695.n4.nabble.com/compute-values-by-condition-in-DF-by-rownames-tp4703351p4703387.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list