citation()
## To cite R in publications use:
##
## R Core Team (2023). _R: A Language and Environment for Statistical
## Computing_. R Foundation for Statistical Computing, Vienna, Austria.
## <https://www.R-project.org/>.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {R: A Language and Environment for Statistical Computing},
## author = {{R Core Team}},
## organization = {R Foundation for Statistical Computing},
## address = {Vienna, Austria},
## year = {2023},
## url = {https://www.R-project.org/},
## }
##
## We have invested a lot of time and effort in creating R, please cite it
## when using it for data analysis. See also 'citation("pkgname")' for
## citing R packages.
citation("dplyr")
## To cite package 'dplyr' in publications use:
##
## Wickham H, François R, Henry L, Müller K, Vaughan D (2023). _dplyr: A
## Grammar of Data Manipulation_. R package version 1.1.4,
## <https://CRAN.R-project.org/package=dplyr>.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {dplyr: A Grammar of Data Manipulation},
## author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller and Davis Vaughan},
## year = {2023},
## note = {R package version 1.1.4},
## url = {https://CRAN.R-project.org/package=dplyr},
## }
carbon <- read.csv(file="C:/Users/ASUS/Downloads/Datasets-Penggabungan data/Experiment_carbon.csv")
carbon
## Treatment Replicate Carbon
## 1 1 1 42.5
## 2 1 3 49.1
## 3 2 1 40.8
## 4 2 2 50.4
## 5 2 3 50.8
## 6 3 1 45.6
## 7 3 2 48.7
## 8 3 3 43.5
nutrients <- read.csv(file="C:/Users/ASUS/Downloads/Datasets-Penggabungan data/Experiment_nutrients.csv")
nutrients
## Treatment Replicate Ammonium Nitrate Nitrite
## 1 1 1 8.2 1.7 0.4
## 2 1 2 6.9 3.6 1.5
## 3 1 3 12.1 2.8 0.8
## 4 2 1 10.5 0.4 0.7
## 5 2 2 8.6 2.7 1.2
## 6 2 3 7.8 4.1 0.9
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Left Join
nutrients %>%
left_join(y=carbon, by=c("Treatment","Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite Carbon
## 1 1 1 8.2 1.7 0.4 42.5
## 2 1 2 6.9 3.6 1.5 NA
## 3 1 3 12.1 2.8 0.8 49.1
## 4 2 1 10.5 0.4 0.7 40.8
## 5 2 2 8.6 2.7 1.2 50.4
## 6 2 3 7.8 4.1 0.9 50.8
Right Join
nutrients %>%
right_join(y=carbon, by=c("Treatment", "Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite Carbon
## 1 1 1 8.2 1.7 0.4 42.5
## 2 1 3 12.1 2.8 0.8 49.1
## 3 2 1 10.5 0.4 0.7 40.8
## 4 2 2 8.6 2.7 1.2 50.4
## 5 2 3 7.8 4.1 0.9 50.8
## 6 3 1 NA NA NA 45.6
## 7 3 2 NA NA NA 48.7
## 8 3 3 NA NA NA 43.5
Inner Join
nutrients %>%
inner_join(y=carbon, by=c("Treatment", "Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite Carbon
## 1 1 1 8.2 1.7 0.4 42.5
## 2 1 3 12.1 2.8 0.8 49.1
## 3 2 1 10.5 0.4 0.7 40.8
## 4 2 2 8.6 2.7 1.2 50.4
## 5 2 3 7.8 4.1 0.9 50.8
Full Join
nutrients %>%
full_join(y=carbon, by=c("Treatment", "Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite Carbon
## 1 1 1 8.2 1.7 0.4 42.5
## 2 1 2 6.9 3.6 1.5 NA
## 3 1 3 12.1 2.8 0.8 49.1
## 4 2 1 10.5 0.4 0.7 40.8
## 5 2 2 8.6 2.7 1.2 50.4
## 6 2 3 7.8 4.1 0.9 50.8
## 7 3 1 NA NA NA 45.6
## 8 3 2 NA NA NA 48.7
## 9 3 3 NA NA NA 43.5
FILTERING JOIN
Semi join
nutrients %>%
semi_join(y=carbon, by=c("Treatment", "Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite
## 1 1 1 8.2 1.7 0.4
## 2 1 3 12.1 2.8 0.8
## 3 2 1 10.5 0.4 0.7
## 4 2 2 8.6 2.7 1.2
## 5 2 3 7.8 4.1 0.9
Anti Join
nutrients %>%
anti_join(y=carbon, by=c("Treatment", "Replicate"))
## Treatment Replicate Ammonium Nitrate Nitrite
## 1 1 2 6.9 3.6 1.5
SOAL 1 : one-to-one relationship merge
library(dplyr)
demographics <- tibble(
id = c("1001", "1002", "1003", "1004"),
dob = as.Date(c("1968-12-14", "1952-08-03", "1949-05-27", "1955-03-12")),
race_eth = c(1, 2, 2, 4))
demographics
## # A tibble: 4 × 3
## id dob race_eth
## <chr> <date> <dbl>
## 1 1001 1968-12-14 1
## 2 1002 1952-08-03 2
## 3 1003 1949-05-27 2
## 4 1004 1955-03-12 4
grip_strength <- tibble(
id = c("1002", "1001", "1003", "1004"),
grip_r = c(32, 28, 32, 22),
grip_l = c(30, 30, 28, 22))
grip_strength
## # A tibble: 4 × 3
## id grip_r grip_l
## <chr> <dbl> <dbl>
## 1 1002 32 30
## 2 1001 28 30
## 3 1003 32 28
## 4 1004 22 22
Gabungkan data frame demographics dan grip_strength dengan fungsi
left_join(), right_join(), full_join(), inner_join(). Lalu amati data
frame gabungan yangdihasilkan.
Left Join
demographics %>%
left_join(y=grip_strength, by=c("id"))
## # A tibble: 4 × 5
## id dob race_eth grip_r grip_l
## <chr> <date> <dbl> <dbl> <dbl>
## 1 1001 1968-12-14 1 28 30
## 2 1002 1952-08-03 2 32 30
## 3 1003 1949-05-27 2 32 28
## 4 1004 1955-03-12 4 22 22
Right Join
demographics %>%
right_join(y=grip_strength, by=c("id"))
## # A tibble: 4 × 5
## id dob race_eth grip_r grip_l
## <chr> <date> <dbl> <dbl> <dbl>
## 1 1001 1968-12-14 1 28 30
## 2 1002 1952-08-03 2 32 30
## 3 1003 1949-05-27 2 32 28
## 4 1004 1955-03-12 4 22 22
Full Join
demographics %>%
full_join(y=grip_strength, by=c("id"))
## # A tibble: 4 × 5
## id dob race_eth grip_r grip_l
## <chr> <date> <dbl> <dbl> <dbl>
## 1 1001 1968-12-14 1 28 30
## 2 1002 1952-08-03 2 32 30
## 3 1003 1949-05-27 2 32 28
## 4 1004 1955-03-12 4 22 22
Inner Join
demographics %>%
inner_join(y=grip_strength, by=c("id"))
## # A tibble: 4 × 5
## id dob race_eth grip_r grip_l
## <chr> <date> <dbl> <dbl> <dbl>
## 1 1001 1968-12-14 1 28 30
## 2 1002 1952-08-03 2 32 30
## 3 1003 1949-05-27 2 32 28
## 4 1004 1955-03-12 4 22 22
Gabungkan data frame grip_strength dan demographics dengan fungsi
left_join(),right_join(), full_join(), inner_join(). Lalu amati data
frame gabungan yang dihasilkan.
Left Join
grip_strength %>%
left_join(y=demographics, by=c("id"))
## # A tibble: 4 × 5
## id grip_r grip_l dob race_eth
## <chr> <dbl> <dbl> <date> <dbl>
## 1 1002 32 30 1952-08-03 2
## 2 1001 28 30 1968-12-14 1
## 3 1003 32 28 1949-05-27 2
## 4 1004 22 22 1955-03-12 4
Right Join
grip_strength %>%
right_join(y=demographics, by=c("id"))
## # A tibble: 4 × 5
## id grip_r grip_l dob race_eth
## <chr> <dbl> <dbl> <date> <dbl>
## 1 1002 32 30 1952-08-03 2
## 2 1001 28 30 1968-12-14 1
## 3 1003 32 28 1949-05-27 2
## 4 1004 22 22 1955-03-12 4
Full Join
grip_strength %>%
full_join(y=demographics, by=c("id"))
## # A tibble: 4 × 5
## id grip_r grip_l dob race_eth
## <chr> <dbl> <dbl> <date> <dbl>
## 1 1002 32 30 1952-08-03 2
## 2 1001 28 30 1968-12-14 1
## 3 1003 32 28 1949-05-27 2
## 4 1004 22 22 1955-03-12 4
Inner Join
grip_strength %>%
inner_join(y=demographics, by=c("id"))
## # A tibble: 4 × 5
## id grip_r grip_l dob race_eth
## <chr> <dbl> <dbl> <date> <dbl>
## 1 1002 32 30 1952-08-03 2
## 2 1001 28 30 1968-12-14 1
## 3 1003 32 28 1949-05-27 2
## 4 1004 22 22 1955-03-12 4