Subset columns based on row value

情到浓时终转凉″ 提交于 2019-12-03 00:25:32

问题


This may be a simple question, but I haven't been able to find any answer. Consider you have a dataframe with n columns with molecular features. In the last row of each column, a coefficient of variance is expressed.

Example data set:

a <- data.frame(matrix(runif(30),ncol=3))
b <- c(50.23,45.23,21)
a<-rbind(a,b)

        X1          X2          X3
1   0.1097075  0.78584027  0.20925033
2   0.6081752  0.39669748  0.65559913
3   0.9912855  0.68462073  0.54741795
4   0.8543848  0.53776889  0.43789447
5   0.2579654  0.92188090  0.61292895
6   0.6203840  0.73152279  0.82866311
7   0.6643195  0.84953926  0.62192976
8   0.5760624  0.30949900  0.11032929
9   0.8888167  0.04530598  0.08089825
10  0.8926815  0.61736284  0.19834310
11 50.2300000 45.23000000 21.00000000

How do I subset so I only get the columns with CV>50 in the last row? So my new data.frame would be:

X1   
1   0.1097075
2   0.6081752
3   0.9912855
4   0.8543848
5   0.2579654
6   0.6203840 
7   0.6643195
8   0.5760624
9   0.8888167
10  0.8926815
11 50.230000

回答1:


We can do

a[,a[nrow(a),]>50,drop=FALSE]


来源:https://stackoverflow.com/questions/40800731/subset-columns-based-on-row-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!