Reputation: 665
I am trying to select columns on multiple conditions: 1. I want to select specific columns but I also want to select variables with specific expressions in their name. I tried this:
df.waste <- w0%>%
select(council_name, Period, year | contains("households" | "Tonnage"))
So I am trying to select the columns "council_name", "Period", "year" AND all those which contain the expressions "household" OR "Tonnage" in their name.
I tried different things all similar to the one above but I'm not quite getting what I want. Thanks!
Upvotes: 1
Views: 208
Reputation: 887048
Another option is grep
in base R
w0[c(1, 2, grep("households|Tonnage", names(w0)))]
Upvotes: 1
Reputation: 3876
Like this?
w0%>%
select(council_name, Period, year | contains(c("households", "Tonnage")))
# A tibble: 1 x 5
council_name Period year householdsabc Tonnageabc
<lgl> <lgl> <lgl> <lgl> <lgl>
1 NA NA NA NA NA
Data
council_name <- NA
Period <- NA
year <- NA
householdsabc <- NA
Tonnageabc <- NA
w0 <- tibble(council_name, Period, year, householdsabc, Tonnageabc)
Upvotes: 2