I have a dataset that has "131" columns and I am trying to drop columns that are not needed for the analysis. Usually I drop the columns by writing the column names one by one. However, I am looking for a way to drop the columns in sequence such that I don't have to write each column's name.
In the data below, let's I am interested in dropping columns a
to d
. How can I do this using dplyr
?
Dummy Data
a = c("a", "b", "c")
b = c("1", "2", "3")
c = c("a1", "b2", "c3")
d = c("F1", "F2", "F3")
e = c("A", "B", "C")
f = c("11", "22", "33")
g = c("A11", "B22", "C33")
Code
library(tidyverse)
df = data.frame(a, b, c, d, e, f, g)
# drop columns a to d
df = df %>% dplyr::.....
CodePudding user response:
On my phone so can’t format properly (apologies), but it seems like this is what you want to do - do not select a:d
library(dplyr)
df %>%
select(-c(a:d))
e f g
1 A 11 A11
2 B 22 B22
3 C 33 C33
CodePudding user response:
Alternative using !
for taking the complement of a set of variables.:
library(dyplr)
df %>%
select(!a:d)
e f g
1 A 11 A11
2 B 22 B22
3 C 33 C33