library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
data <- tibble::as.tibble(starwars)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
head(data)
## # A tibble: 6 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 4 Darth Va… 202 136 none white yellow 41.9 male mascu…
## 5 Leia Org… 150 49 brown light brown 19 fema… femin…
## 6 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
starwars
## # A tibble: 87 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke Sk… 172 77 blond fair blue 19 male mascu…
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 4 Darth V… 202 136 none white yellow 41.9 male mascu…
## 5 Leia Or… 150 49 brown light brown 19 fema… femin…
## 6 Owen La… 178 120 brown, gr… light blue 52 male mascu…
## 7 Beru Wh… 165 75 brown light blue 47 fema… femin…
## 8 R5-D4 97 32 <NA> white, red red NA none mascu…
## 9 Biggs D… 183 84 black light brown 24 male mascu…
## 10 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male mascu…
## # ℹ 77 more rows
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
catatan : Head() digunakan untuk mempermudah proses observasi data.
Merangkum data menjadi rata-rata dari data tinggi dan berat karakter yang ada pada dataset Starwars. Data yang digunakan hanya 20 data menggunakan head(, 20).
summarised_data <- data %>%
summarise( ,
avg_height = mean(height,na.rm = TRUE),
avg_mass = mean(mass,na.rm = TRUE)
)
head(summarised_data, 20)
## # A tibble: 1 × 2
## avg_height avg_mass
## <dbl> <dbl>
## 1 175. 97.3
Mengurutkan data berdasarkan height secara menurun.
arranged_data <- data %>%
arrange(desc(height))
print(arranged_data, n = Inf)
## # A tibble: 87 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Yarael… 264 NA none white yellow NA male mascu…
## 2 Tarfful 234 136 brown brown blue NA male mascu…
## 3 Lama Su 229 88 none grey black NA male mascu…
## 4 Chewba… 228 112 brown unknown blue 200 male mascu…
## 5 Roos T… 224 82 none grey orange NA male mascu…
## 6 Grievo… 216 159 none brown, wh… green, y… NA male mascu…
## 7 Taun We 213 NA none grey black NA fema… femin…
## 8 Rugor … 206 NA none green orange NA male mascu…
## 9 Tion M… 206 80 none grey black NA male mascu…
## 10 Darth … 202 136 none white yellow 41.9 male mascu…
## 11 IG-88 200 140 none metal red 15 none mascu…
## 12 Ki-Adi… 198 82 white pale yellow 92 male mascu…
## 13 Dexter… 198 102 none brown yellow NA male mascu…
## 14 Jar Ja… 196 66 none orange orange 52 male mascu…
## 15 Kit Fi… 196 87 none green black NA male mascu…
## 16 Mas Am… 196 NA none blue blue NA male mascu…
## 17 Qui-Go… 193 89 brown fair blue 92 male mascu…
## 18 Dooku 193 80 white fair brown 102 male mascu…
## 19 Wat Ta… 193 48 none green, gr… unknown NA male mascu…
## 20 Nute G… 191 90 none mottled g… red NA male mascu…
## 21 Bail P… 191 NA black tan brown 67 male mascu…
## 22 San Hi… 191 NA none grey gold NA male mascu…
## 23 Bossk 190 113 none green red 53 male mascu…
## 24 Anakin… 188 84 blond fair blue 41.9 male mascu…
## 25 Mace W… 188 84 none dark brown 72 male mascu…
## 26 Saesee… 188 NA none pale orange NA male mascu…
## 27 Plo Ko… 188 80 none orange black 22 male mascu…
## 28 Raymus… 188 79 brown light brown NA male mascu…
## 29 Padmé … 185 45 brown light brown 46 fema… femin…
## 30 Gregar… 185 85 black dark brown NA <NA> <NA>
## 31 Adi Ga… 184 50 none dark blue NA fema… femin…
## 32 Biggs … 183 84 black light brown 24 male mascu…
## 33 Boba F… 183 78.2 black fair brown 31.5 male mascu…
## 34 Ric Ol… 183 NA brown fair blue NA male mascu…
## 35 Quarsh… 183 NA black dark brown 62 male mascu…
## 36 Cliegg… 183 NA brown fair blue 82 male mascu…
## 37 Poggle… 183 80 none green yellow NA male mascu…
## 38 Jango … 183 79 black tan brown 66 male mascu…
## 39 Obi-Wa… 182 77 auburn, w… fair blue-gray 57 male mascu…
## 40 Wilhuf… 180 NA auburn, g… fair blue 64 male mascu…
## 41 Han So… 180 80 brown fair brown 29 male mascu…
## 42 Jek To… 180 110 brown fair blue NA <NA> <NA>
## 43 Ackbar 180 83 none brown mot… orange 41 male mascu…
## 44 Bib Fo… 180 NA none pale pink NA male mascu…
## 45 Owen L… 178 120 brown, gr… light blue 52 male mascu…
## 46 Ayla S… 178 55 none blue hazel 48 fema… femin…
## 47 Shaak … 178 57 none red, blue… black NA fema… femin…
## 48 Sly Mo… 178 48 none pale white NA <NA> <NA>
## 49 Lando … 177 79 black dark brown 31 male mascu…
## 50 Jabba … 175 1358 <NA> green-tan… orange 600 herm… mascu…
## 51 Lobot 175 79 none light blue 37 male mascu…
## 52 Darth … 175 80 none red yellow 54 male mascu…
## 53 Greedo 173 74 <NA> green black 44 male mascu…
## 54 Luke S… 172 77 blond fair blue 19 male mascu…
## 55 Eeth K… 171 NA black brown brown NA male mascu…
## 56 Wedge … 170 77 brown fair hazel 21 male mascu…
## 57 Palpat… 170 75 grey pale yellow 82 male mascu…
## 58 Finis … 170 NA blond fair blue 91 male mascu…
## 59 Lumina… 170 56.2 black yellow blue 58 fema… femin…
## 60 Zam We… 168 55 blonde fair, gre… yellow NA fema… femin…
## 61 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 62 Jocast… 167 NA white fair blue NA fema… femin…
## 63 Barris… 166 50 black yellow blue 40 fema… femin…
## 64 Beru W… 165 75 brown light blue 47 fema… femin…
## 65 Dormé 165 NA brown light brown NA fema… femin…
## 66 Shmi S… 163 NA black fair brown 72 fema… femin…
## 67 Ben Qu… 163 65 none grey, gre… orange NA male mascu…
## 68 Nien N… 160 68 none grey black NA male mascu…
## 69 Cordé 157 NA brown light brown NA <NA> <NA>
## 70 Leia O… 150 49 brown light brown 19 fema… femin…
## 71 Mon Mo… 150 NA auburn fair blue 48 fema… femin…
## 72 Watto 137 NA black blue, grey yellow NA male mascu…
## 73 Gasgano 122 NA none white, bl… black NA male mascu…
## 74 Sebulba 112 40 none grey, red orange NA male mascu…
## 75 R5-D4 97 32 <NA> white, red red NA none mascu…
## 76 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 77 R4-P17 96 NA none silver, r… red, blue NA none femin…
## 78 Dud Bo… 94 45 none blue, grey yellow NA male mascu…
## 79 Wicket… 88 20 brown brown brown 8 male mascu…
## 80 Ratts … 79 15 none grey, blue unknown NA male mascu…
## 81 Yoda 66 17 white green brown 896 male mascu…
## 82 Arvel … NA NA brown fair brown NA male mascu…
## 83 Finn NA NA black dark dark NA male mascu…
## 84 Rey NA NA brown light hazel NA fema… femin…
## 85 Poe Da… NA NA brown light brown NA male mascu…
## 86 BB8 NA NA none none black NA none mascu…
## 87 Captai… NA NA none none unknown NA fema… femin…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
Mengurutkan data berdasarkan mass.
arranged_data2 <- data %>%
arrange(mass)
print(arranged_data2, n = Inf)
## # A tibble: 87 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Ratts … 79 15 none grey, blue unknown NA male mascu…
## 2 Yoda 66 17 white green brown 896 male mascu…
## 3 Wicket… 88 20 brown brown brown 8 male mascu…
## 4 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 5 R5-D4 97 32 <NA> white, red red NA none mascu…
## 6 Sebulba 112 40 none grey, red orange NA male mascu…
## 7 Padmé … 185 45 brown light brown 46 fema… femin…
## 8 Dud Bo… 94 45 none blue, grey yellow NA male mascu…
## 9 Wat Ta… 193 48 none green, gr… unknown NA male mascu…
## 10 Sly Mo… 178 48 none pale white NA <NA> <NA>
## 11 Leia O… 150 49 brown light brown 19 fema… femin…
## 12 Adi Ga… 184 50 none dark blue NA fema… femin…
## 13 Barris… 166 50 black yellow blue 40 fema… femin…
## 14 Ayla S… 178 55 none blue hazel 48 fema… femin…
## 15 Zam We… 168 55 blonde fair, gre… yellow NA fema… femin…
## 16 Lumina… 170 56.2 black yellow blue 58 fema… femin…
## 17 Shaak … 178 57 none red, blue… black NA fema… femin…
## 18 Ben Qu… 163 65 none grey, gre… orange NA male mascu…
## 19 Jar Ja… 196 66 none orange orange 52 male mascu…
## 20 Nien N… 160 68 none grey black NA male mascu…
## 21 Greedo 173 74 <NA> green black 44 male mascu…
## 22 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 23 Beru W… 165 75 brown light blue 47 fema… femin…
## 24 Palpat… 170 75 grey pale yellow 82 male mascu…
## 25 Luke S… 172 77 blond fair blue 19 male mascu…
## 26 Obi-Wa… 182 77 auburn, w… fair blue-gray 57 male mascu…
## 27 Wedge … 170 77 brown fair hazel 21 male mascu…
## 28 Boba F… 183 78.2 black fair brown 31.5 male mascu…
## 29 Lando … 177 79 black dark brown 31 male mascu…
## 30 Lobot 175 79 none light blue 37 male mascu…
## 31 Jango … 183 79 black tan brown 66 male mascu…
## 32 Raymus… 188 79 brown light brown NA male mascu…
## 33 Han So… 180 80 brown fair brown 29 male mascu…
## 34 Darth … 175 80 none red yellow 54 male mascu…
## 35 Plo Ko… 188 80 none orange black 22 male mascu…
## 36 Poggle… 183 80 none green yellow NA male mascu…
## 37 Dooku 193 80 white fair brown 102 male mascu…
## 38 Tion M… 206 80 none grey black NA male mascu…
## 39 Roos T… 224 82 none grey orange NA male mascu…
## 40 Ki-Adi… 198 82 white pale yellow 92 male mascu…
## 41 Ackbar 180 83 none brown mot… orange 41 male mascu…
## 42 Biggs … 183 84 black light brown 24 male mascu…
## 43 Anakin… 188 84 blond fair blue 41.9 male mascu…
## 44 Mace W… 188 84 none dark brown 72 male mascu…
## 45 Gregar… 185 85 black dark brown NA <NA> <NA>
## 46 Kit Fi… 196 87 none green black NA male mascu…
## 47 Lama Su 229 88 none grey black NA male mascu…
## 48 Qui-Go… 193 89 brown fair blue 92 male mascu…
## 49 Nute G… 191 90 none mottled g… red NA male mascu…
## 50 Dexter… 198 102 none brown yellow NA male mascu…
## 51 Jek To… 180 110 brown fair blue NA <NA> <NA>
## 52 Chewba… 228 112 brown unknown blue 200 male mascu…
## 53 Bossk 190 113 none green red 53 male mascu…
## 54 Owen L… 178 120 brown, gr… light blue 52 male mascu…
## 55 Darth … 202 136 none white yellow 41.9 male mascu…
## 56 Tarfful 234 136 brown brown blue NA male mascu…
## 57 IG-88 200 140 none metal red 15 none mascu…
## 58 Grievo… 216 159 none brown, wh… green, y… NA male mascu…
## 59 Jabba … 175 1358 <NA> green-tan… orange 600 herm… mascu…
## 60 Wilhuf… 180 NA auburn, g… fair blue 64 male mascu…
## 61 Mon Mo… 150 NA auburn fair blue 48 fema… femin…
## 62 Arvel … NA NA brown fair brown NA male mascu…
## 63 Finis … 170 NA blond fair blue 91 male mascu…
## 64 Rugor … 206 NA none green orange NA male mascu…
## 65 Ric Ol… 183 NA brown fair blue NA male mascu…
## 66 Watto 137 NA black blue, grey yellow NA male mascu…
## 67 Quarsh… 183 NA black dark brown 62 male mascu…
## 68 Shmi S… 163 NA black fair brown 72 fema… femin…
## 69 Bib Fo… 180 NA none pale pink NA male mascu…
## 70 Gasgano 122 NA none white, bl… black NA male mascu…
## 71 Eeth K… 171 NA black brown brown NA male mascu…
## 72 Saesee… 188 NA none pale orange NA male mascu…
## 73 Yarael… 264 NA none white yellow NA male mascu…
## 74 Mas Am… 196 NA none blue blue NA male mascu…
## 75 Cordé 157 NA brown light brown NA <NA> <NA>
## 76 Cliegg… 183 NA brown fair blue 82 male mascu…
## 77 Dormé 165 NA brown light brown NA fema… femin…
## 78 Bail P… 191 NA black tan brown 67 male mascu…
## 79 Taun We 213 NA none grey black NA fema… femin…
## 80 Jocast… 167 NA white fair blue NA fema… femin…
## 81 R4-P17 96 NA none silver, r… red, blue NA none femin…
## 82 San Hi… 191 NA none grey gold NA male mascu…
## 83 Finn NA NA black dark dark NA male mascu…
## 84 Rey NA NA brown light hazel NA fema… femin…
## 85 Poe Da… NA NA brown light brown NA male mascu…
## 86 BB8 NA NA none none black NA none mascu…
## 87 Captai… NA NA none none unknown NA fema… femin…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
Menampilkan data yang memiliki height di atas 150.
filtered_data <- data %>%
filter( height >150)
head(filtered_data)
## # A tibble: 6 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 3 Darth Va… 202 136 none white yellow 41.9 male mascu…
## 4 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
## 5 Beru Whi… 165 75 brown light blue 47 fema… femin…
## 6 Biggs Da… 183 84 black light brown 24 male mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
Menampilkan data yang memiliki mass di bawah 100
filtered_data2 <- data %>%
filter(mass >100)
head(filtered_data2)
## # A tibble: 6 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Darth Va… 202 136 none white yellow 41.9 male mascu…
## 2 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
## 3 Chewbacca 228 112 brown unknown blue 200 male mascu…
## 4 Jabba De… 175 1358 <NA> green-tan… orange 600 herm… mascu…
## 5 Jek Tono… 180 110 brown fair blue NA <NA> <NA>
## 6 IG-88 200 140 none metal red 15 none mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
Membuat sebuah data set yang baru, bernama BMI dengan menggunakan data yang sudah ada, yaitu height dan mass.
Starwars2 <- data %>% mutate(BMI=(mass/((height/100)^2)))
head(Starwars2)
## # A tibble: 6 × 15
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 4 Darth Va… 202 136 none white yellow 41.9 male mascu…
## 5 Leia Org… 150 49 brown light brown 19 fema… femin…
## 6 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
## # ℹ 6 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>, BMI <dbl>
Untuk memilih data yang akan ditampilkan(atau yang akan dibuang). Memilih hanya data name, height, dan mass yang akan ditampilkan. Dan data yang ditampilkan hanya 10 data teratas menggunakan head(, 10).
Starwars3 <- data %>%
select(name, height, mass)
head(Starwars3, 10)
## # A tibble: 10 × 3
## name height mass
## <chr> <int> <dbl>
## 1 Luke Skywalker 172 77
## 2 C-3PO 167 75
## 3 R2-D2 96 32
## 4 Darth Vader 202 136
## 5 Leia Organa 150 49
## 6 Owen Lars 178 120
## 7 Beru Whitesun Lars 165 75
## 8 R5-D4 97 32
## 9 Biggs Darklighter 183 84
## 10 Obi-Wan Kenobi 182 77
Penggunaan fungsi Mutate() dan Select().
Starwars3 <- data %>% mutate(BMI=(mass/((height/100)^2))) %>% select(name, height, mass, BMI)
head(Starwars3, 10)
## # A tibble: 10 × 4
## name height mass BMI
## <chr> <int> <dbl> <dbl>
## 1 Luke Skywalker 172 77 26.0
## 2 C-3PO 167 75 26.9
## 3 R2-D2 96 32 34.7
## 4 Darth Vader 202 136 33.3
## 5 Leia Organa 150 49 21.8
## 6 Owen Lars 178 120 37.9
## 7 Beru Whitesun Lars 165 75 27.5
## 8 R5-D4 97 32 34.0
## 9 Biggs Darklighter 183 84 25.1
## 10 Obi-Wan Kenobi 182 77 23.2