[英]Finding and Excluding Columns of Dataframe with Certain Patterns in Header Names with R
I'm trying to find a solution to a data frame subsetting need I have. 我正在尝试找到我需要的数据帧子集的解决方案。
I have a data frame that with the following general structure I have named wrkDat
我有一个数据框,具有以下常规结构,将其命名为
wrkDat
A R2_A B R2_B ..... Z R2_Z
1.1 0.99 2.2 0.97 ..... 26.6 0.96
1.9 0.89 4.2 0.99 ..... 12.8 0.78
I would like to be able to selectively subset so I don't have the R2 columns. 我希望能够有选择地子集,所以我没有R2列。 Initially I was thinking something like the following would work, but it does not.
最初,我在想类似以下内容的方法会起作用,但事实并非如此。
selected <- "^R2." %in% colnames(wrkDat)
wrkDat <- wrkDat[,!selected]
Thank you to Mitra and plafort for getting me thinking about the grep functions. 感谢Mitra,感谢您让我思考grep函数。 I found the following that worked perfectly.
我发现以下效果很好。 I don't know what I was thinking trying to use regex patterns with %in%....
我不知道我尝试使用%in%使用正则表达式模式的想法。
selected <- grepl("^R2.", colnames(wrkDat))
wrkDat <- wrkDat[,!selected]
wrkDat <- wrkDat[,-grep("^R2.",colnames(wrkDat)]
应该可以
You could do: 您可以这样做:
library(dplyr)
df %>% select(-starts_with("R2"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.