我有这个数据框:
dput(DF)
structure(list(Time = structure(1:4, .Label = c("1/29/2015 2:00",
"1/29/2015 2:10", "1/29/2015 2:20", "1/29/2015 2:30"), class = "factor"),
WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Content.Match = structure(c(1L,
1L, 1L, 1L), .Label = "n/a", class = "factor"), WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Status = structure(c(1L,
1L, 1L, 1L), .Label = "n/a", class = "factor"), WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Value = c(12L,
12L, 12L, 12L), WTAD..SNMP..AppTier.BIGIP.SNMP.Memory.on.Web01.Content.Match = structure(c(1L,
1L, 1L, 1L), .Label = "n/a", class = "factor")), .Names = c("Time",
"WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Content.Match",
"WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Status",
"WTAD..SNMP..AppTier.BIGIP.SNMP.CPU.5min.avg.on.Web01.Value",
"WTAD..SNMP..AppTier.BIGIP.SNMP.Memory.on.Web01.Content.Match"
), class = "data.frame", row.names = c(NA, -4L))
我正在尝试包含此内容的列:CPU.5min.avg.on。*。Value“
library(dplyr)
df<-select(df, Time, contains("CPU.5min.avg.on.*.Value"))
这项工作在Windows R但不是Linux。我有什么想法吗?
答案 0 :(得分:2)
Base R解决方案:
df[,c("Time",colnames(df)[sapply(colnames(df), function(u) grepl("CPU.5min.avg.on.*.Value",u))])]
答案 1 :(得分:2)
dplyr
解决方案:
select(df, Time, matches('CPU.5min.avg.on.*.Value'))
实际上,我很困惑你的解决方案在Windows下工作。 ?select
文档说:
contains(x,ignore.case = TRUE):选择名称的所有变量 包含x
matches(x,ignore.case = TRUE):选择名称的所有变量 匹配正则表达式x
并且您尝试匹配代码中的正则表达式,因此它不应该在任何操作系统下与contain
一起使用。