removing column from a df with their names in r

In R, there are different ways to remove a column from a dataframe using its name. One way is to use the subset() function, which will return a dataframe without the selected columns:

main.r
# Creating a sample dataframe
df <- data.frame(a=c(1,2,3), b=c(4,5,6), c=c(7,8,9))

# Using subset() to remove column 'b'
df_without_b <- subset(df, select = -b)
162 chars
6 lines

Another way to remove columns is using indexing. We can select all the columns except the ones we want to remove by using a vector containing their names with a minus sign in front, and assigning it to the original dataframe:

main.r
# Creating a sample dataframe
df <- data.frame(a=c(1,2,3), b=c(4,5,6), c=c(7,8,9))

# Removing columns 'a' and 'c'
df_without_a_c <- df[, -c(1, 3)]
148 chars
6 lines

Another very popular way to manipulate dataframes in R is by using the dplyr package. A simple way to remove columns is by using the select() function with the - operator before the column names:

main.r
# Loading dplyr package
library(dplyr)

# Creating a sample dataframe
df <- data.frame(a=c(1,2,3), b=c(4,5,6), c=c(7,8,9))

# Using select() to remove column 'b'
df_without_b <- select(df, -b)
193 chars
9 lines

All of these methods achieve the same result, which is removing the selected column(s) from the original dataframe.

gistlibby LogSnag