- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 drop data frame columns in R by using column name?
Columns of a data frame can be dropped by creating an object of columns that we want to drop or an object of columns that we want to keep.
> df <- data.frame( Var1 =1:10, Var2 =11:20, Var3 =21:30, Var4 =31:40 ) We can drop columns Var1 and Var2 using the below code: drops <- c("Var1","Var2") > df[ , !(names(df) %in% drops)] Var3 Var4 1 21 31 2 22 32 3 23 33 4 24 34 5 25 35 6 26 36 7 27 37 8 28 38 9 29 39 10 30 40
Alternatively, we can do the same by using keeps function that will keep the variables we want to have in our data frame as shown below −
> keeps <- c("Var3", "Var4") > df[keeps] Var3 Var4 1 21 31 2 22 32 3 23 33 4 24 34 5 25 35 6 26 36 7 27 37 8 28 38 9 29 39 10 30 40
- How to add name to data frame columns in R?
- Extract columns with a string in column name of an R data frame.
- How to combine multiple columns into one in R data frame without using column names?
- How to create scatterplot using data frame columns in R?
- How to split a data frame by column in R?
- How to convert multiple columns into single column in an R data frame?
- How to add columns with square of each column in R data frame?
- How to subset a data frame by excluding a column using dplyr in R?
- How to extract only factor columns name from an R data frame?
- How to subset row values based on columns name in R data frame?
- How to remove first character from column name in R data frame?
- How to sort a data frame in R by multiple columns together?
- How to divide data frame rows by number of columns in R?
- How to add a column between columns or after last column in an R data frame?
- How to convert a data frame into two column data frame with values and column name as variable in R?