我有一个看起来像这样的data.table
> dput(DT)
A B C A B C D
1: 1 2 3 3 5 6 7
2: 2 1 3 2 1 3 4
这是dput
DT <- structure(list(A = 1:2, B = c(2L, 1L), C = c(3L, 3L), A = c(3L,
2L), B = c(5L, 1L), C = c(6L, 3L), D = c(7L, 4L)), .Names = c("A",
"B", "C", "A", "B", "C", "D"), row.names = c(NA, -2L), class = c("data.table",
"data.frame"))
基本上,我想根据标题对它们进行子集化。所以对于标题“B”,我会这样做:
subset(DT,,grep(unique(names(DT))[2],names(DT)))
B B
1: 2 2
2: 1 1
正如您所看到的,值是错误的,因为第二列只是第一列的重复。我希望得到这个:
B B
1: 2 5
2: 1 1
有人可以帮我吗?
答案 0 :(得分:9)
以下替代方案对我有用:
pos <- grep("B", names(DT))
DT[, pos, with = FALSE]
# B B
# 1: 2 5
# 2: 1 1
DT[, grep("B", names(DT)), with = FALSE]
# B B
# 1: 2 5
# 2: 1 1
DT[, names(DT) %in% unique(names(DT))[2], with = FALSE]
# B B
# 1: 2 5
# 2: 1 1
这也有效:
DT[, .SD, .SDcols = grep("B", names(DT))]
# B B
# 1: 2 5
# 2: 1 1