标题有点措辞,但我认为正确地描述了我想要解决的问题。我有一个包含以下列名的数据框:
> colnames(mydf)
[1] "rank" "team.ID"
[3] "team.City" "team.Name"
[5] "team.Abbreviation" "stats.GamesPlayed.@abbreviation"
[7] "stats.GamesPlayed.#text" "stats.AtBats.@category"
[9] "stats.AtBats.@abbreviation" "stats.AtBats.#text"
[11] "stats.Runs.@category" "stats.Runs.@abbreviation"
[13] "stats.Runs.#text" "stats.Hits.@category"
[15] "stats.Hits.@abbreviation" "stats.Hits.#text"
这个完整的数据帧宽约400列,但遵循以下结构:
我想使用一个dplyr行来获取前5列和任何带有#text后缀的列,但以下内容不起作用:
mydf <- mydf %>% dplyr::select(contains(c('rank', 'team.', '#text')))
有关如何实现这一目标的任何想法?谢谢!
答案 0 :(得分:2)
您可以将多个参数传递给select()
。例如
mydf %>% select(rank:team.Abbreviation, ends_with("#text"))
使用
进行测试mydf <- data.frame("rank"=1, "team.ID"=1,
"team.City"="A", "team.Name"="A",
"team.Abbreviation"="A", "stats.GamesPlayed.@abbreviation"="A",
"stats.GamesPlayed.#text"="A", "stats.AtBats.@category"="A",
"stats.AtBats.@abbreviation"="A", "stats.AtBats.#text"="A",
"stats.Runs.@category"="A", "stats.Runs.@abbreviation"="A",
"stats.Runs.#text"="A", "stats.Hits.@category"="A",
"stats.Hits.@abbreviation"="A", "stats.Hits.#text"="A", check.names=FALSE)