+5 votes
in Programming Languages by (40.5k points)
I want to drop some columns from an R data frame. Which R function should I use for it?

1 Answer

+2 votes
by (349k points)
selected by
 
Best answer

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  D
1 1 11 21 31
2 2 12 22 32
3 3 13 23 33
4 4 14 24 34
5 5 15 25 35

> df1 <- subset(df, select = -A) #To drop one column
> df1
   B  C  D
1 11 21 31
2 12 22 32
3 13 23 33
4 14 24 34
5 15 25 35

> df1 <- subset(df, select = -c(A,D)) #To drop multiple columns
> df1
   B  C
1 11 21
2 12 22
3 13 23
4 14 24
5 15 25

Using select() function

> library(dplyr)
> df1 <- df %>% select(-A)    # to drop one column
> df1
   B  C  D
1 11 21 31
2 12 22 32
3 13 23 33
4 14 24 34
5 15 25 35
> df1 <- df %>% select(-c(A,D))    # to drop multiple columns
> df1
   B  C
1 11 21
2 12 22
3 13 23
4 14 24
5 15 25
> df1 <- df %>% select(-c(1,2)) # using column number
> df1
   C  D
1 21 31
2 22 32
3 23 33
4 24 34
5 25 35

Related questions

+5 votes
1 answer
+5 votes
1 answer
+4 votes
1 answer

...