2013-04-08 56 views
1

我在R中遇到以下問題:在R中選擇具有特定值的列

我正在處理一個巨大的矩陣。一些列包含值'零',這導致我在進一步的工作中出現問題。

因此,我想確定colums,至少包含一個'零'值。

任何想法如何做到這一點?

在此先感謝!

喬治

+0

thx to all。它的工作原理:D – elJorge 2013-04-08 17:56:20

回答

2

如果你有一個大的矩陣,那麼這可能會比適用的解決方案更快: mat[,colSums(mat==0)<0.5]

0

可以說你的矩陣稱爲X,

x = matrix(runif(300), nrow=10) 

來獲取至少有1個零列的索引:

ix = apply(x, MARGIN=2, function(col){any(col==0)}) 
0

試試這個

set.seed(1) # for generating an example 
mat <- matrix(sample(0:9, 100, TRUE), ncol=10) 
ind <- apply(mat, 2, function(x) any(x==0)) # making an index 
mat[,ind] # columns containing at least one zero 
mat[,!ind] # cols without zeros 
相關問題