The dplyr package in R is a structure of data manipulation that provides a uniform set of verbs, helping to resolve the most frequent data manipulation hurdles.
The dplyr package performs the steps given below quicker and in an easier fashion:
- By limiting the choices the focus can now be more on data manipulation difficulties.
- There are uncomplicated “verbs”, functions present for tackling every common data manipulation and the thoughts can be translated into code faster.
- There are valuable backends and hence waiting time for the computer reduces.
Important Verb Functions
dplyr package provides various important function that can be used for Data Manipulation. These are:
- filter() Function: For choosing cases and using their values as a base for doing so.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )d# Finding rows with NA valued%>%filter(is.na(ht))# Finding rows with no NA valued%>%filter(!is.na(ht))chevron_rightfilter_noneOutput:
# A tibble: 4 x 4 name age ht school 1 Abhi 7 46 yes 2 Bhavesh 5 NA yes 3 Chaman 9 NA no 4 Dimri 16 69 no # A tibble: 2 x 4 name age ht school 1 Bhavesh 5 NA yes 2 Chaman 9 NA no # A tibble: 2 x 4 name age ht school 1 Abhi 7 46 yes 2 Dimri 16 69 no - arrange(): For reordering of the cases.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )# Arranging name according to the aged.name<-arrange(d, age)print(d.name)chevron_rightfilter_noneOutput:
# A tibble: 4 x 4 name age ht school 1 Bhavesh 5 NA yes 2 Abhi 7 46 yes 3 Chaman 9 NA no 4 Dimri 16 69 no - select() and rename(): For choosing variables and using their names as a base for doing so.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )# startswith() function to print only ht dataselect(d, starts_with("ht"))# -startswith() function to print everything except ht dataselect(d,-starts_with("ht"))# Printing column 1 to 2select(d,1:2)# Printing data of column heading containing 'a'select(d, contains("a"))# Printing data of column heading which matches 'na'select(d, matches("na"))chevron_rightfilter_noneOutput:
# A tibble: 4 x 1 ht 1 46 2 NA 3 NA 4 69 # A tibble: 4 x 3 name age school 1 Abhi 7 yes 2 Bhavesh 5 yes 3 Chaman 9 no 4 Dimri 16 no # A tibble: 4 x 2 name age 1 Abhi 7 2 Bhavesh 5 3 Chaman 9 4 Dimri 16 # A tibble: 4 x 2 name age 1 Abhi 7 2 Bhavesh 5 3 Chaman 9 4 Dimri 16 # A tibble: 4 x 1 name 1 Abhi 2 Bhavesh 3 Chaman 4 Dimri - mutate() and transmute(): Addition of new variables which are the functions of prevailing variables.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )# Calculating a variable x3 which is sum of height# and age printing with ht and agemutate(d, x3=ht+age)# Calculating a variable x3 which is sum of height# and age printing without ht and agetransmute(d, x3=ht+age)chevron_rightfilter_noneOutput:
# A tibble: 4 x 5 name age ht school x3 1 Abhi 7 46 yes 53 2 Bhavesh 5 NA yes NA 3 Chaman 9 NA no NA 4 Dimri 16 69 no 85 # A tibble: 4 x 1 x3 1 53 2 NA 3 NA 4 85 > - summarise(): Condensing various values to one value.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )# Calculating mean of agesummarise(d, mean=mean(age))# Calculating min of agesummarise(d, med=min(age))# Calculating max of agesummarise(d, med=max(age))# Calculating median of agesummarise(d, med=median(age))chevron_rightfilter_noneOutput:
# A tibble: 1 x 1 mean 1 9.25 # A tibble: 1 x 1 med 1 5 # A tibble: 1 x 1 med 1 16 # A tibble: 1 x 1 med 1 8 - sample_n() and sample_frac(): For taking random specimens.
# Create a data frame with missing datad <-data.frame( name=c("Abhi","Bhavesh","Chaman","Dimri"),age=c(7,5,9,16),ht=c(46, NA, NA,69),school=c("yes","yes","no","no") )# Printing three rowssample_n(d,3)# Printing 50 % of the rowssample_frac(d,0.50)chevron_rightfilter_noneOutput:
# A tibble: 3 x 4 name age ht school 1 Abhi 7 46 yes 2 Bhavesh 5 NA yes 3 Chaman 9 NA no # A tibble: 2 x 4 name age ht school 1 Dimri 16 69 no 2 Bhavesh 5 NA yes
Recommended Posts:
- tidyr Package in R Programming
- Using ggplot2 package in R Programming
- Shiny Package in R Programming
- Esquisse Package in R Programming
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Set or View the Graphics Palette in R Programming - palette() Function
- Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
- Intersection of Two Objects in R Programming - intersect() Function
- Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
- Clustering in R Programming
- Compute Variance and Standard Deviation of a value in R Programming - var() and sd() Function
- Compute Density of the Distribution Function in R Programming - dunif() Function
- Compute Randomly Drawn F Density in R Programming - rf() Function
- Data Handling in R Programming
- Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
- Print the Value of an Object in R Programming - identity() Function
- Check if Two Objects are Equal in R Programming - setequal() Function
- Random Forest with Parallel Computing in R Programming
- R - Object Oriented Programming
- Check for Presence of Common Elements between Objects in R Programming - is.element() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

