Remove a Column in a DataFrame in R

Here are 3 ways to remove a single column in a DataFrame in R:

Using subset()

df <- subset(df, select = -column_name_to_remove)

Using the indexing operator []

df <- df[, -which(names(df) == "column_name_to_remove")]

Using the column index:

df <- subset(df, select = -column_index_to_remove)

Here are additional 3 ways to remove multiple columns in a DataFrame in R:

Using subset()

df <- subset(df, select = -c(column_name_to_remove_1, column_name_to_remove_2, ...))

Using the indexing operator []

df <- df[, -which(names(df) %in% c("column_name_to_remove_1", "column_name_to_remove_2", ...))]

Using the column indices:

df <- subset(df, select = -c(column_index_to_remove_1, column_index_to_remove_2, ...))

Examples of Removing column/s in a DataFrame in R

Example 1: Remove a single column in a DataFrame in R

To start, create a DataFrame in R with 4 columns: Colors, Shapes, Sizes and Length:

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

print(df)

Here is the DataFrame with the 4 columns:

  Colors    Shapes   Sizes  Length
1    red    square   small      10
2  green    circle  medium      25
3   blue  triangle   large     150
4 yellow rectangle   small       5
5 orange      cone  medium      45

To remove the “Shapes” column using subset():

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- subset(df, select = -Shapes)

print(df)

Here is the new DataFrame without the “Shapes” column:

  Colors  Sizes Length
1    red  small     10
2  green medium     25
3   blue  large    150
4 yellow  small      5
5 orange medium     45

You’ll get the same results using the indexing operator [] as follows:

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- df[, -which(names(df) == "Shapes")]

print(df)

The result:

  Colors  Sizes Length
1    red  small     10
2  green medium     25
3   blue  large    150
4 yellow  small      5
5 orange medium     45

Or by using the column index:

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- subset(df, select = -2)

print(df)

The result:

  Colors  Sizes Length
1    red  small     10
2  green medium     25
3   blue  large    150
4 yellow  small      5
5 orange medium     45

Example 2: Remove multiple columns in a DataFrame in R

To remove the “Shapes” and the “Sizes” columns using subset():

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- subset(df, select = -c(Shapes, Sizes))

print(df)

The result:

  Colors Length
1    red     10
2  green     25
3   blue    150
4 yellow      5
5 orange     45

Or by the indexing operator []:

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- df[, -which(names(df) %in% c("Shapes", "Sizes"))]

print(df)

The result:

  Colors Length
1    red     10
2  green     25
3   blue    150
4 yellow      5
5 orange     45

Or by the column index:

df <- data.frame(Colors = c("red", "green", "blue", "yellow", "orange"),
                 Shapes = c("square", "circle", "triangle", "rectangle", "cone"),
                 Sizes = c("small", "medium", "large", "small", "medium"),
                 Length = c(10, 25, 150, 5, 45)
                )

df <- subset(df, select = -c(2, 3))

print(df)

The result:

  Colors Length
1    red     10
2  green     25
3   blue    150
4 yellow      5
5 orange     45