Professional Documents
Culture Documents
R Package Dplyr Comparison of Functions
R Package Dplyr Comparison of Functions
+1.732.947.4119
About Us
Categories
DATA SCIENCE
• Data Science (27)
R Package dplyr Comparison of • AI / Machine Learning (6)
functions • Big Data (2)
# filter data with Wind > 7.0 for the month of May
#dplyr way
filter(airquality,Wind > 7.0, Month == 5)
Digital Life Sciences Healthcare Staffing Company Contact Us Knowledge Center
# base way
#first
airquality[airquality$Wind > 7.0 & airquality$Month == 5,]
#second
subset(airquality,Wind > 7.0 & Month == 5)
Use caution when using subset. For further readings look at the
stackoverflow
thread, http://stackoverflow.com/questions/9860090/in-r-why-
is-better-than-subset
Mutate function
# base function
# somehow i find it easy to use than mutate
airquality$TempInC<-((Temp - 32) * 5 /9)
Arrange function
# dplyr--arrange
arrange(airquality,Month,desc(Temp))
# base--order()
airquality[order(airquality$Month,-airquality$Temp),]
# Note - for ordering desc
# Both base and summarise give you same output for normal
df/tbl
mean(airquality$Temp)
## [1] 77.88235
summarise(airquality, mean(Temp))
## mean(Temp)
## 1 77.88235
grouped_table<-group_by(airquality,Month)
head(grouped_table)
## [1] 153 6
dim(grouped_table)
Digital Life Sciences Healthcare Staffing Company Contact Us Knowledge Center
## [1] 153 6
Both have same dimensions and you can see the header
records of grouped_table which looks same as original
dataset. But grouped_table is grouped on top of Month
variable. You can see the 'Groups' section denoting the
variable(s) used for group_by function.
Generally you can use more than one variable to group and
ask Summarise fn to give output. Now we ask for
average(mean) using summarise and mean fns and compare
the results.
mean(grouped_table$Temp)
## [1] 77.88235
summarise(grouped_table,mean(Temp))
## # A tibble: 5 x 2
## Month mean(Temp)
##
## 1 5 65.54839
## 2 6 79.10000
## 3 7 83.90323
## 4 8 83.96774
## 5 9 76.90000
dplyr::distinct vs base::unique
From the name you can understand both unique and distinct
functions. Both gives you the unique/distinct values but unique
works with list too (of course).
distinct(airquality,Month)
## Month
## 1 5
## 2 6
## 3 7
## 4 8
## 5 9
unique(airquality$Month)
dplyr::sample_n/sample_frac vs
base::sample
sample_n(airquality,size=2)
sample_frac(airquality,size=0.01)
Piping
## # A tibble: 5 x 2
## Month mean_wind
##
## 1 5 11.622581
## 2 6 10.266667
## 3 9 10.180000
## 4 7 8.941935
## 5 8 8.793548
This completes our introduction part for dplyr. This will help you
to start working with data and have fun.!!
Office Locations
United India Canad
Corporate Headquarters:
Rang Technologies Inc.
15 Corporate Pl S, Suite# 356,
Piscataway, NJ 08854
Phone
+1.732.947.4119
About Us Digital