library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4.9000 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ 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(datasets)
data("beavers")
BT<-tibble::as_tibble(beaver1)
view(BT)
class(BT)
## [1] "tbl_df" "tbl" "data.frame"
head(BT)
## # A tibble: 6 × 4
## day time temp activ
## <dbl> <dbl> <dbl> <dbl>
## 1 346 840 36.3 0
## 2 346 850 36.3 0
## 3 346 900 36.4 0
## 4 346 910 36.4 0
## 5 346 920 36.6 0
## 6 346 930 36.7 0
glimpse(BT)
## Rows: 114
## Columns: 4
## $ day <dbl> 346, 346, 346, 346, 346, 346, 346, 346, 346, 346, 346, 346, 346,…
## $ time <dbl> 840, 850, 900, 910, 920, 930, 940, 950, 1000, 1010, 1020, 1030, …
## $ temp <dbl> 36.33, 36.34, 36.35, 36.42, 36.55, 36.69, 36.71, 36.75, 36.81, 3…
## $ activ <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
summary(BT)
## day time temp activ
## Min. :346.0 Min. : 0.0 Min. :36.33 Min. :0.00000
## 1st Qu.:346.0 1st Qu.: 932.5 1st Qu.:36.76 1st Qu.:0.00000
## Median :346.0 Median :1415.0 Median :36.87 Median :0.00000
## Mean :346.2 Mean :1312.0 Mean :36.86 Mean :0.05263
## 3rd Qu.:346.0 3rd Qu.:1887.5 3rd Qu.:36.96 3rd Qu.:0.00000
## Max. :347.0 Max. :2350.0 Max. :37.53 Max. :1.00000
#Fungsi untuk mengetahui banyaknya missing value
BT %>%
filter_all(any_vars(is.na(.))) %>%
nrow
## [1] 0
view(BT)
#Fungsi arrange untuk mengurutkan temperatur badan dari tertinggi ke terendah
BT %>% arrange(desc(temp))
## # A tibble: 114 × 4
## day time temp activ
## <dbl> <dbl> <dbl> <dbl>
## 1 346 2150 37.5 1
## 2 346 2230 37.2 1
## 3 346 2300 37.2 1
## 4 346 2200 37.2 0
## 5 346 2250 37.2 0
## 6 346 2210 37.2 0
## 7 346 2240 37.2 0
## 8 346 2320 37.2 0
## 9 346 2330 37.2 0
## 10 347 340 37.2 1
## # ℹ 104 more rows
#Fungsi filter untuk menampilkan data temperatur badan lebih dari 35 Celsius
BT %>% filter(temp>="35")
## # A tibble: 114 × 4
## day time temp activ
## <dbl> <dbl> <dbl> <dbl>
## 1 346 840 36.3 0
## 2 346 850 36.3 0
## 3 346 900 36.4 0
## 4 346 910 36.4 0
## 5 346 920 36.6 0
## 6 346 930 36.7 0
## 7 346 940 36.7 0
## 8 346 950 36.8 0
## 9 346 1000 36.8 0
## 10 346 1010 36.9 0
## # ℹ 104 more rows
#Fungsi select untuk menampilkan tabel tanpa kolom time
BT %>% select(-time)
## # A tibble: 114 × 3
## day temp activ
## <dbl> <dbl> <dbl>
## 1 346 36.3 0
## 2 346 36.3 0
## 3 346 36.4 0
## 4 346 36.4 0
## 5 346 36.6 0
## 6 346 36.7 0
## 7 346 36.7 0
## 8 346 36.8 0
## 9 346 36.8 0
## 10 346 36.9 0
## # ℹ 104 more rows
#Fungsi mutate untuk menambah kolom keterangan demam atau tidak
BT %>% mutate(demam=temp>="37")
## # A tibble: 114 × 5
## day time temp activ demam
## <dbl> <dbl> <dbl> <dbl> <lgl>
## 1 346 840 36.3 0 FALSE
## 2 346 850 36.3 0 FALSE
## 3 346 900 36.4 0 FALSE
## 4 346 910 36.4 0 FALSE
## 5 346 920 36.6 0 FALSE
## 6 346 930 36.7 0 FALSE
## 7 346 940 36.7 0 FALSE
## 8 346 950 36.8 0 FALSE
## 9 346 1000 36.8 0 FALSE
## 10 346 1010 36.9 0 FALSE
## # ℹ 104 more rows
#Menampilkan data temperatur badan yang lebih dari 37 derajat celcius tanpa melihat kolom waktu
BT %>% select(-time) %>% group_by(temp) %>% filter(temp>="37")
## # A tibble: 20 × 3
## # Groups: temp [15]
## day temp activ
## <dbl> <dbl> <dbl>
## 1 346 37 0
## 2 346 37.1 1
## 3 346 37.0 0
## 4 346 37 0
## 5 346 37 0
## 6 346 37.0 0
## 7 346 37.1 1
## 8 346 37.1 0
## 9 346 37.0 0
## 10 346 37.5 1
## 11 346 37.2 0
## 12 346 37.2 0
## 13 346 37.2 1
## 14 346 37.2 0
## 15 346 37.2 0
## 16 346 37.2 1
## 17 346 37.1 0
## 18 346 37.2 0
## 19 346 37.2 0
## 20 347 37.2 1
BT %>%
select(temp) %>%
summarise(mean_temp = mean(temp, na.rm = TRUE))
## # A tibble: 1 × 1
## mean_temp
## <dbl>
## 1 36.9