Column(s) of an R dataframe can be dropped using either the subset() function or the select() function of library "dplyr".
Using subset() function:
> df <- data.frame("A"= 1:5, "B"=11:15, "C"=21:25, "D"=31:35)> df A B C D1 1 11 21 312 2 12 22 323 3 13 23 334 4 14 24 345 5 15 25 35> df1 <- subset(df, select = -A) #To drop one column> df1 B C D1 11 21 312 12 22 323 13 23 334 14 24 345 15 25 35> df1 <- subset(df, select = -c(A,D)) #To drop multiple columns> df1 B C1 11 212 12 223 13 234 14 245 15 25
Using select() function
> library(dplyr)> df1 <- df %>% select(-A) # to drop one column> df1 B C D1 11 21 312 12 22 323 13 23 334 14 24 345 15 25 35> df1 <- df %>% select(-c(A,D)) # to drop multiple columns> df1 B C1 11 212 12 223 13 234 14 245 15 25> df1 <- df %>% select(-c(1,2)) # using column number> df1 C D1 21 312 22 323 23 334 24 345 25 35