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 ✔ 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
?tidyverse
## starting httpd help server ... done
library(datasets)
data(iris)
iris <- tibble::as.tibble(iris)
## 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.
install.packages("dplyr")
## Warning: package 'dplyr' is in use and will not be installed
library(dplyr)
class(iris)
## [1] "tbl_df" "tbl" "data.frame"
view(iris)
head(iris)
## # A tibble: 6 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
mean(iris$Sepal.Length) == iris$Sepal.Length %>% mean () #%>% tuh dpl yr ada di package tidyverse
## [1] TRUE
mean(iris$Sepal.Length)
## [1] 5.843333
iris$Sepal.Length %>% mean ()
## [1] 5.843333
mean(iris$Petal.Length)
## [1] 3.758
x <- c(0.109, 0.359, 0.63, 0.996, 0.515, 0.142, 0.017, 0.829, 0.907)
x
## [1] 0.109 0.359 0.630 0.996 0.515 0.142 0.017 0.829 0.907
round(exp(diff(log(x))),1)
## [1] 3.3 1.8 1.6 0.5 0.3 0.1 48.8 1.1
x%>% log() %>%
diff() %>%
exp() %>%
round(1)
## [1] 3.3 1.8 1.6 0.5 0.3 0.1 48.8 1.1
#summarize ##menghitung rata-rata sepal length setiap species
iris %>% group_by(Species) %>% summarize (mean=mean(Sepal.Length))
## # A tibble: 3 × 2
## Species mean
## <fct> <dbl>
## 1 setosa 5.01
## 2 versicolor 5.94
## 3 virginica 6.59
##mengurutkan berdasarkan peubah sepale.length dari nilai terkecil
iris %>% arrange (Sepal.Length)
## # A tibble: 150 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 4.3 3 1.1 0.1 setosa
## 2 4.4 2.9 1.4 0.2 setosa
## 3 4.4 3 1.3 0.2 setosa
## 4 4.4 3.2 1.3 0.2 setosa
## 5 4.5 2.3 1.3 0.3 setosa
## 6 4.6 3.1 1.5 0.2 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 4.6 3.6 1 0.2 setosa
## 9 4.6 3.2 1.4 0.2 setosa
## 10 4.7 3.2 1.3 0.2 setosa
## # ℹ 140 more rows
#filter ##mengambil data dari Petal.Width,Species,Petal.Length
iris %>% select(Petal.Width,Species,Petal.Length)
## # A tibble: 150 × 3
## Petal.Width Species Petal.Length
## <dbl> <fct> <dbl>
## 1 0.2 setosa 1.4
## 2 0.2 setosa 1.4
## 3 0.2 setosa 1.3
## 4 0.2 setosa 1.5
## 5 0.2 setosa 1.4
## 6 0.4 setosa 1.7
## 7 0.3 setosa 1.4
## 8 0.2 setosa 1.5
## 9 0.2 setosa 1.4
## 10 0.1 setosa 1.5
## # ℹ 140 more rows
#mutate ##menambah peubah baru
iris %>% mutate(Sepal.Length=Sepal.Width)
## # A tibble: 150 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 3.5 3.5 1.4 0.2 setosa
## 2 3 3 1.4 0.2 setosa
## 3 3.2 3.2 1.3 0.2 setosa
## 4 3.1 3.1 1.5 0.2 setosa
## 5 3.6 3.6 1.4 0.2 setosa
## 6 3.9 3.9 1.7 0.4 setosa
## 7 3.4 3.4 1.4 0.3 setosa
## 8 3.4 3.4 1.5 0.2 setosa
## 9 2.9 2.9 1.4 0.2 setosa
## 10 3.1 3.1 1.5 0.1 setosa
## # ℹ 140 more rows