- 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 extract columns of a data frame in R using dplyr package?
Example
> x <- seq(0, 20, by = 1) > y <- dnorm(x, mean = 5, sd = 1) > z <- dbinom(x, size=100, p=0.5) > df<-data.frame(x,y,z)
Let’s say we want to extract columns y and z. It can be done in two ways as follows −
First
> library(dplyr) > df %>% select(y,z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 3.989423e-01 5.939138e-23 7 2.419707e-01 9.403635e-22 8 5.399097e-02 1.262774e-20 9 4.431848e-03 1.467975e-19 10 1.338302e-04 1.500596e-18 11 1.486720e-06 1.365543e-17 12 6.075883e-09 1.117262e-16 13 9.134720e-12 8.286361e-16 14 5.052271e-15 5.609229e-15 15 1.027977e-18 3.485735e-14 16 7.694599e-23 1.998488e-13 17 2.118819e-27 1.061697e-12 18 2.146384e-32 5.246031e-12 19 7.998828e-38 2.419003e-11 20 1.096607e-43 1.043991e-10 21 5.530710e-50 4.228163e-10
Second
> select(df,y,z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 3.989423e-01 5.939138e-23 7 2.419707e-01 9.403635e-22 8 5.399097e-02 1.262774e-20 9 4.431848e-03 1.467975e-19 10 1.338302e-04 1.500596e-18 11 1.486720e-06 1.365543e-17 12 6.075883e-09 1.117262e-16 13 9.134720e-12 8.286361e-16 14 5.052271e-15 5.609229e-15 15 1.027977e-18 3.485735e-14 16 7.694599e-23 1.998488e-13 17 2.118819e-27 1.061697e-12 18 2.146384e-32 5.246031e-12 19 7.998828e-38 2.419003e-11 20 1.096607e-43 1.043991e-10 21 5.530710e-50 4.228163e-10
- Related Articles
- How to remove multiple rows from an R data frame using dplyr package?
- How to convert numeric columns to factor using dplyr package in R?
- How to extract data frame columns stored in a list in R?
- How to subset rows of data frame without NA using dplyr in R?
- How to collapse data frame rows in R by summing using dplyr?
- How to subset a data frame by excluding a column using dplyr in R?
- How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- How to extract only factor columns name from an R data frame?
- How to extract unique values in multiple columns in an R data frame using a single line code?
- Extract columns with a string in column name of an R data frame.
- How to find the mean of row values in an R data frame using dplyr?
- How to create a rank variable using mutate function of dplyr package in R?
- How to create scatterplot using data frame columns in R?
- How to extract a single column of an R data frame as a data frame?

Advertisements