问题
I need to be able to extract columns that contain exact string that I am looking for. For example, I have this data frame x:
structure(list(Time = structure(1L, .Label = "1/1/2015", class = "factor"),
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB. = 3555L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.Free.MB. = 55L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Swap.Free.MB. = 44L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Cache.Free.MB. = 66L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Swap.And.Cache.Free.MB. = 44L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Percent.Free = 44L,
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Round.Trip.Time = 44L), .Names = c("Time",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB.",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.Free.MB.",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Swap.Free.MB.",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Cache.Free.MB.",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Swap.And.Cache.Free.MB.",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Percent.Free",
"WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Round.Trip.Time"
), class = "data.frame", row.names = c(NA, -1L))
I need to only extract the column that contains this exact match ".Total.Phys.Mem.MB."
When I do this:
x[,grepl(".Total.Phys.Mem.MB.", colnames(x)[2:ncol(x)])]
I dont get the column that contains this string in it ".Total.Phys.Mem.MB.". Is there a better way to extract the columns that contain the string in R?
回答1:
library(dplyr)
select(x, contains(".Total.Phys.Mem.MB."))
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB.
1 3555
回答2:
Unless fixed=TRUE
is defined, grepl
recognizes the pattern as a regular expression; and in regex the dot is a character of special meaning which must be escaped to match a literal.
> x[grepl("\\.Total\\.Phys\\.Mem\\.MB\\.", colnames(x))]
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB.
1 3555
OR
> x[grepl('.Total.Phys.Mem.MB.', colnames(x), fixed=TRUE)]
WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB.
1 3555
来源:https://stackoverflow.com/questions/28099189/how-do-you-extract-the-columns-that-contain-a-certain-text-string-in-r