A data frame has the following variables:
names(df1)
"var1_a" "var1_b" "var2_a" "var2_b" "var3_a" "var4_a"
Am using dplyr to remove variables as follows - one var at a time:
df2 <- df1 %>% select(-starts_with(("var1"), everything()))
df2 <- df2 %>% select(-starts_with(("var2"), everything()))
Using "dplyr", is there a way to concatenate the two line into one?
You could construct a regex expression as a per your desire and use the matches
wrapper in order to filter you columns accordingly (with some modifications from @BenBolker)
df2 <- df1 %>% select(-matches("^var[12]"))
Or just modify df1
in place using the %<>%
pipe
library(magrittr)
df1 %<>% select(-matches("^var[12]"))
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.