How To Remove A Column From An R Data Frame? - Tutorialspoint

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • 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? R ProgrammingServer Side ProgrammingProgramming

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 Nizamuddin Siddiqui Nizamuddin Siddiqui Updated on: 2020-07-06T14:30:39+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » How To Remove Columns In R