2014-03-26 82 views
5

我有以下row.names矩陣:子集由row.names矩陣

"X1" "X5" "X33" "X37" "X52" "X566" 

現在我只需要選擇匹配列表中的條目這行,說:

include_list <- c("X1", "X5", "X33") 

我想我會做這樣的事情:

data.subset <- subset(data, row.names == include_list) 

然而,這個特殊的代碼似乎並沒有做的工作。我該如何執行子集化?

回答

16

設置一些假數據:

m <- matrix(1:30, 6, 5) 
rownames(m) <- c("X1", "X5", "X33", "X37", "X52", "X566") 
m 
#  [,1] [,2] [,3] [,4] [,5] 
# X1  1 7 13 19 25 
# X5  2 8 14 20 26 
# X33  3 9 15 21 27 
# X37  4 10 16 22 28 
# X52  5 11 17 23 29 
# X566 6 12 18 24 30 

這可能是最簡單的用矩陣索引到子集([):

subset()功能
include_list <- c("X1", "X5", "X33") 
m[include_list, ] 
#  [,1] [,2] [,3] [,4] [,5] 
# X1  1 7 13 19 25 
# X5  2 8 14 20 26 
# X33 3 9 15 21 27 

備選:

subset(m, rownames(m) %in% include_list) 
#  [,1] [,2] [,3] [,4] [,5] 
# X1  1 7 13 19 25 
# X5  2 8 14 20 26 
# X33 3 9 15 21 27 
3

這也似乎工作:

include_list <- head(read.csv("/Users/histelheim/include_list.csv", header = FALSE)) 
include_list <- c(do.call("cbind", include_list)) 
data[row.names(data) %in% include_list, ]