[R] compute values by condition in DF by rownames
JS Huang
js.huang at protective.com
Tue Feb 17 05:30:11 CET 2015
Hi,
I hope that someone can provide a better way to implement it. This is my
implementation.
> 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
> test
function(x)
{
tempMatrix <- matrix(0,nrow=3,ncol=3)
lev <- levels(x$X2)
for (i in 1:length(lev))
{
tempMatrix[i,1] = sum(ifelse(abs(x[x$X2==lev[i],2])>0.2,1,0))
tempMatrix[i,2] = sum(ifelse(abs(x[x$X2==lev[i],3])>0.2,1,0))
tempMatrix[i,3] = sum(ifelse(abs(x[x$X2==lev[i],4])>0.2,1,0))
}
result <- data.frame(lev,tempMatrix)
names(result) <- c("X2","gbm_tcga","lusc_tcga","tcga_pub")
return(result)
}
> test(data)
X2 gbm_tcga lusc_tcga tcga_pub
1 gbm_tcga 4 1 0
2 lusc_tcga 1 4 0
3 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-tp4703351p4703374.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list