Reputation: 10626
I have this data frame that I form reading a url. When I read the url, I get some mixed data. I would like to be able to do two things:
subset the df so that colnames will only include CPU.Percent in them (sometimes for some reason I get memory or disk utilization, I need to remove those columns).
colnames should only include the server names, for example dc1server, dc1web etc.
Here is my data frame called df:
dput(df)
structure(list(Time = structure(1:2, .Label = c("1/13/2015",
"1/14/2015"), class = "factor"), WEB..Linux..Total.CPU.Percent.Utilization.on.dc1server.Utilization = structure(1:2, .Label = c("3.67%",
"3.75%"), class = "factor"), WEB..Linux..Total.CPU.Percent.Utilization.on.dc1web2.Utilization = structure(c(2L,
1L), .Label = c("13.08%", "13.25%"), class = "factor"), WTAD..Linux..Virtual.Memory.on.dc1server1.Percent.Used = structure(c(2L,
1L), .Label = c("9%", "9.42%"), class = "factor")), .Names = c("Time",
"WEB..Linux..Total.CPU.Percent.Utilization.on.dc1server.Utilization",
"WEB..Linux..Total.CPU.Percent.Utilization.on.dc1web2.Utilization",
"WTAD..Linux..Virtual.Memory.on.dc1server1.Percent.Used"), class = "data.frame", row.names = c(NA,
-2L))
For 1.
I tried this:
data<-subset(df, grep("CPU.Percent", colnames(df))
is not working
for 2:
I tried:
colnames(df)<-gsub(colnames(df), grep(".(dc1.*).",colnames(df))
no luck, any ideas what I may be doing here?
Upvotes: 0
Views: 59
Reputation: 2400
As akrun stated,
data <- df[, grepl("CPU.Percent", colnames(df))]
solves the first problem.
And for the second, try this
colnames(df) <- sub(".*\\.(dc[^\\.]*)\\..*", "\\1", colnames(df))
Note that changing the column names will change the way that the first bit of code works. Just be sure that you do them in the right order.
If you do the colnames change first, then the first problem solution would be:
data <- df[,2:3]
Upvotes: 3