这是我的数据(它是一个输出):
structure(list(`dat$Material` = structure(1L, .Label = "stuff", class = "factor"),
Past.Due = structure(NA_integer_, .Names = "x", .Label = character(0), class = "factor"),
D01 = structure(NA_integer_, .Names = "x", .Label = character(0), class = "factor"),
D02 = structure(NA_integer_, .Names = "x", .Label = character(0), class = "factor"),
D03 = structure(1L, .Names = "x", .Label = "100.00", class = "factor"),
D04 = structure(1L, .Names = "x", .Label = "100.00", class = "factor"),
D05 = structure(1L, .Names = "x", .Label = "100.00", class = "factor"),
Wk.1.PD = structure(NA_integer_, .Names = "x", .Label = character(0), class = "factor"),
D06 = structure(1L, .Names = "x", .Label = "100.00", class = "factor"),
D07 = structure(1L, .Names = "x", .Label = "100.00", class = "factor")), .Names = c("dat$Material",
"Past.Due", "D01", "D02", "D03", "D04", "D05", "Wk.1.PD", "D06",
"D07"), row.names = "x", class = "data.frame")
基本上我想要提取所有名称都在" D ##"之后的列,所以D01,D02,D03等。我尝试使用grep
和grepl
但我是正则表达式的新手,无法弄清楚如何让它发挥作用。
我试过了:
dat[grepl("D\d\d",names(dat))]
但那并没有奏效。我的实际数据是比我提供的列更多的列,因此在列号中进行硬编码并手动提取列是不行的。有任何提示或建议吗?