df <- data.frame(vin, make, model, year, category)
I would like to delete/remove columns, "year" and "category", and put them in a new view
df <- data.frame(vin, make, model, year, category)
I would like to delete/remove columns, "year" and "category", and put them in a new view
We can use setdiff
to get all the columns except the 'year' and 'category'.
df1 <- df[setdiff(colnames(df), c('year', 'category'))]
df1
# vin make model
#1 1 A D
#2 2 B E
#3 3 C F
Including the comments from @Frank and @Ben Bolker.
We can assign the columns to NULL
df$year <- NULL
df$category <- NULL
Or use subset
from base R
or select
from dplyr
subset(df, select=-c(year, category))
library(dplyr)
select(df, -year, -category)
df <- data.frame(vin=1:3, make=LETTERS[1:3], model=LETTERS[4:6],
year=1991:1993, category= paste0('GR', 1:3))