This tuturial is the part of the dplyr training series. Here is the YouTube Video link for this tutuorial.
Video link https://youtu.be/isWquPc2Uew
Here is the link to the complete series on DPLYR
https://www.youtube.com/playlist?list=PLkHcMTpvAaXVJzyRSytUn3nSK92TJphxR
dplyr is a great tool to use in R.
The commands may look long and overwhelming to someone not using dplyr but that is not the case.
Once you learn the basics of it then it is very intuitive to use. Just like making a sentence once you have learnt the basic words of a language.
For beginners or experienced R users wanting to learn various commands of dplyr.
We will be covering all practical aspects of dplyr::case_when command in this. This tutorial is part of a series of tutorials on all practical aspects of dplyr All YouTube videos are available in a single playlist on YouTube.
https://www.youtube.com/playlist?list=PLkHcMTpvAaXVJzyRSytUn3nSK92TJphxR
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
Let us create some data for patient ages.
Contain the age and weight of the patients. PatientID is the unique key in this dataset.
d1 <- data.frame( PatientID = c('P001','P002','P003')
, Age = c(23,12,5)
, Weight = c(80, 65, 78)
)
d1
Contain the Systolic blood pressure (SBP) and diastolic blood pressure (DBP) of the patients. PatientID is the unique key in this dataset.
d2 <- data.frame(PatientID = c('P001','P002','P004')
, SBP = c(120, 80, 45)
, DBP = c(80, 70, 30)
)
d2
Contain the heart rate reading of the patients. PatientID is the unique key in this dataset.
d3 <- data.frame(PatientID = c('P001','P005','P006')
, HeartRate = c(65, 65, 72)
)
d3
Contains the patient outcome ( died or discharged (live)). PatID is the unique key in this dataset.
Notice that in the first three datasets we had the PatientID as the key and in this dataset PatID is the key.
d4 <- data.frame(PatID = c('P001','P004','P007')
, Outcome = c('Died','Died','Discharged')
)
d4
This is the simple syntax using dplyr. You provide the names of your datasets.
The position of the first and second table is important. The first table (d1) in the example below is considered as right side table and all the rows of this table will appear in your joined results.
As in both the datasets “PatientID” is the key so we can just specify it once saying by =“PatientID”. If the keys are different in both the cases then you have to specify both the keys as shown in subsequent examples in this document.
# Right Join
p_right <- dplyr::right_join(d1,d2, by ="PatientID")
p_right
Use this syntax when joining more than two tables Joining two tables
# dplyr
p_right2 <- d1%>% # This is the right table
dplyr::right_join(d2, by = "PatientID")
p_right2
Multiple tables can be joined in a single command.
Notice the for d2 and d3 we specified the keys as by = “PatientID” which is the same as saying by =c(“PatientID” = “PatientID”)
But for d4 we had to say by =c(“PatientID” = “PatID”)
# dplyr
p_right3 <- d1%>%
dplyr::right_join(d2, by = "PatientID")%>%
dplyr::right_join(d3, by = "PatientID")%>%
dplyr::right_join(d4, by =c("PatientID" = "PatID"))
p_right3
Watch our complete tutorial on all aspects of DPLYR.
https://www.youtube.com/playlist?list=PLkHcMTpvAaXVJzyRSytUn3nSK92TJphxR