The title is a bit worded, but I think decently describes what I am trying to solve. I have a dataframe with the following column names:
> 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"
This full dataframe is ~400 columns wide, but follows this structure:
I would like to use one dplyr line to grab the first 5 columns AND any columns with a #text suffix, but the following doesn't work:
mydf <- mydf %>% dplyr::select(contains(c('rank', 'team.', '#text')))
Any thoughts on how this could be accomplished? Thanks!
You can pass multiple parameters to select()
. For example
mydf %>% select(rank:team.Abbreviation, ends_with("#text"))
Tested with
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.