如何使用部分匹配列名称对数据帧进行子集化

时间:2014-12-06 14:36:31

标签: r

我有一个很大的数据框架。我想使用与某些列名称的前部部分匹配的名称列表来对数据框进行子集化。我不知道如何编写代码。 例如,列表名称为:lst <- c("LD08.1593","LD09.10917","LD10.10226","M05.353086","Thore")

数据框列: ...,"LC10096.2", "LD08.1593.s1", "LD08.1593.s2","LD08.1692.1","LD08.1692.2","LD09.10917.s1","LD09.10917.s2","LD10.10226-s1","LD10.10226-s2","LEC.12.6056.70","LEC.12.6113.02","M05.353086","Thore_t1","Thore_t5", ... 我希望子集数据框只包含"LD08.1593.s1","LD08.1593.s2","LD09.10917.s1","LD09.10917.s2","LD10.10226-s1","LD10.10226-s2","M05.353086","Thore_t1","Thore_t5"

感谢。

1 个答案:

答案 0 :(得分:2)

可能你可以试试

 dat[,grep(paste0("^",paste(lst, collapse="|")), colnames(dat))]

数据

set.seed(42)
dat <- as.data.frame(matrix(sample(1:25,14*10, replace=TRUE), ncol=14))

colnames(dat) <- c("LC10096.2", "LD08.1593.s1", "LD08.1593.s2","LD08.1692.1",
"LD08.1692.2","LD09.10917.s1","LD09.10917.s2","LD10.10226-s1",
"LD10.10226-s2","LEC.12.6056.70","LEC.12.6113.02","M05.353086",
"Thore_t1","Thore_t5")

lst <- c("LD08.1593","LD09.10917","LD10.10226","M05.353086","Thore")