- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove a column from an R data frame?
This can be easily done by using subset function.
Example
> df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20) > df x y z a 1 1 6 11 16 2 2 7 12 17 3 3 8 13 18 4 4 9 14 19 5 5 10 15 20
To remove only one column
> df <- subset (df, select = -x) > df y z a 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20
To remove two columns
> df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20) > df <- subset (df, select = -c(x,y)) > df z a 1 11 16 2 12 17 3 13 18 4 14 19 5 15 20
To remove a range of columns
> df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20) > df <- subset (df, select = -c(x:z)) > df a 1 16 2 17 3 18 4 19 5 20
To remove separate columns
> df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20) > df <- subset (df, select = -c(x,z:a)) > df y 1 6 2 7 3 8 4 9 5 10
- Related Articles
- How to remove column names from an R data frame?
- How to remove underscore from column names of an R data frame?
- How to remove a character in an R data frame column?
- How to remove a common suffix from column names in an R data frame?
- How to remove single quote from string column in an R data frame?
- How to remove empty rows from an R data frame?
- How to remove first character from column name in R data frame?
- How to remove continuously repeated duplicates in an R data frame column?
- How to remove last few rows from an R data frame?
- How to remove a column from a data frame that contains same value in R?
- How to remove everything before values starting after underscore from column values of an R data frame?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to remove rows based on blanks in a column from a data frame in R?
- How to standardized a column in an R data frame?
- How to extract a single column of an R data frame as a data frame?

Advertisements