remove mutltiple columns from a df in r

To remove multiple columns from a data frame in R, you can use the dplyr package's select() function to select only the columns you want to keep.

Here's an example using the mtcars dataset:

main.r
library(dplyr)

# create a new data frame without the 'cyl', 'am', and 'gear' columns
mtcars_new <- mtcars %>% 
  select(-cyl, -am, -gear)

# view the first few rows of the new data frame
head(mtcars_new)
205 chars
9 lines

In this example, we used the - operator to exclude the columns we want to remove. The resulting data frame mtcars_new only contains the columns that were not removed.

gistlibby LogSnag