Skip to content

Data Science Tutorials

For Data Science Learners

  • Quantiles by Group calculation in R
    Quantiles by Group calculation in R with examples R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • Best Data Visualization Books Course
Remove Columns from a data frame

How to Remove Columns from a data frame in R

Posted on June 4June 4 By Admin No Comments on How to Remove Columns from a data frame in R

Remove Columns from a data frame, you may occasionally need to remove one or more columns from a data frame. Fortunately, the select() method from the dplyr package makes this simple.

Remove Rows from the data frame in R – Data Science Tutorials

library(dplyr)

Using the data frame below, this tutorial demonstrates numerous examples of how to utilize this function in practice.

Remove Columns from a data frame

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'),
points = c(122, 144, 154, 155, 120, 218, 229),
assists = c(43, 55, 77, 18, 114, NA,29))

Now we can view the data frame

One way ANOVA Example in R-Quick Guide – Data Science Tutorials

df
    player points assists
1     P1    122      43
2     P2    144      55
3     P3    154      77
4     P4    155      18
5     P5    120     114
6     P6    218      NA
7     P7    229      29

Approach 1: Remove Columns by Name

How to delete columns from a data frame by name is demonstrated in the following code.

delete the ‘points’ column

How to perform the Kruskal-Wallis test in R? – Data Science Tutorials

df %>% select(-points)
player assists
1     P1      43
2     P2      55
3     P3      77
4     P4      18
5     P5     114
6     P6      NA
7     P7      29

Approach 2: Remove Columns in the List

The code below demonstrates how to delete columns from a data frame that belong to a certain list.

‘Points’ and ‘player’ columns should be removed.

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

df %>% select(-one_of('points', 'player'))
    assists
1      43
2      55
3      77
4      18
5     114
6      NA
7      29

Approach 3: Remove Columns in Range

To remove all columns in the range from ‘position’ to ‘points,’ use the following code.

delete columns from ‘player’ to ‘points’ in the range.

df %>% select(-(player:points))
   assists
1      43
2      55
3      77
4      18
5     114
6      NA
7      29

Approach 4: Remove Columns that Contain a Phrase

The code below demonstrates how to delete all columns containing the word ‘points.’

glm function in r-Generalized Linear Models – Data Science Tutorials

delete columns with the word ‘points’ in them.

df %>% select(-contains('points'))
   player assists
1     P1      43
2     P2      55
3     P3      77
4     P4      18
5     P5     114
6     P6      NA
7     P7      29

Approach 5: Remove Columns that Start with Certain Letters

To eliminate all columns that begin with the letters ‘po,’ use the following code.

Hypothesis Testing Examples-Quick Overview – Data Science Tutorials

delete columns that begin with the letter ‘po’

df %>% select(-starts_with('po'))
player assists
1     P1      43
2     P2      55
3     P3      77
4     P4      18
5     P5     114
6     P6      NA
7     P7      29

Approach 6: Remove Columns that End with Certain Letters

To eliminate all columns that finish in the letter’s,’ use the following code:

‘s’-ending columns should be removed.

How to perform the MANOVA test in R? – Data Science Tutorials

df %>% select(-ends_with('s'))
    player
1     P1
2     P2
3     P3
4     P4
5     P5
6     P6
7     P7

Approach 7: Remove Columns by Position

The code below demonstrates how to remove columns from certain locations:

Columns 1 and 3 should be removed.

df %>% select(-1, -3)
   points
1    122
2    144
3    154
4    155
5    120
6    218
7    229

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Remove Rows from the data frame in R
Next Post: How to add columns to a data frame in R

Related Posts

  • ggpairs in R
    ggpairs in R R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • bootstrapping-in-r
    Bootstrapping in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • Extract values from vector in R: dplyr R
  • Data Science Strategies for Improving Customer Experience in R
    Data Science Strategies for Improving Customer Experience in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Steps to Mastering Natural Language Processing
    Steps to Mastering Natural Language Processing Machine Learning
  • How to add Axes to Plot in R R
  • Mastering the map() Function in R R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Positive or Negative in R R
  • How to Find the Size of a Data Frame in R R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • Ogive Graph in R
    Ogive Graph in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme