Mencari data

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
library(datasets)
data()
data(Orange)
Orange
##    Tree  age circumference
## 1     1  118            30
## 2     1  484            58
## 3     1  664            87
## 4     1 1004           115
## 5     1 1231           120
## 6     1 1372           142
## 7     1 1582           145
## 8     2  118            33
## 9     2  484            69
## 10    2  664           111
## 11    2 1004           156
## 12    2 1231           172
## 13    2 1372           203
## 14    2 1582           203
## 15    3  118            30
## 16    3  484            51
## 17    3  664            75
## 18    3 1004           108
## 19    3 1231           115
## 20    3 1372           139
## 21    3 1582           140
## 22    4  118            32
## 23    4  484            62
## 24    4  664           112
## 25    4 1004           167
## 26    4 1231           179
## 27    4 1372           209
## 28    4 1582           214
## 29    5  118            30
## 30    5  484            49
## 31    5  664            81
## 32    5 1004           125
## 33    5 1231           142
## 34    5 1372           174
## 35    5 1582           177
Orange <- tibble::as.tibble(Orange)
## 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.
Orange
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 1       118            30
##  2 1       484            58
##  3 1       664            87
##  4 1      1004           115
##  5 1      1231           120
##  6 1      1372           142
##  7 1      1582           145
##  8 2       118            33
##  9 2       484            69
## 10 2       664           111
## # ℹ 25 more rows
class(Orange)
## [1] "tbl_df"     "tbl"        "data.frame"
view(Orange)
glimpse(Orange)
## Rows: 35
## Columns: 3
## $ Tree          <ord> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,…
## $ age           <dbl> 118, 484, 664, 1004, 1231, 1372, 1582, 118, 484, 664, 10…
## $ circumference <dbl> 30, 58, 87, 115, 120, 142, 145, 33, 69, 111, 156, 172, 2…
head(Orange) 
## # A tibble: 6 × 3
##   Tree    age circumference
##   <ord> <dbl>         <dbl>
## 1 1       118            30
## 2 1       484            58
## 3 1       664            87
## 4 1      1004           115
## 5 1      1231           120
## 6 1      1372           142
mean(Orange$circumference)
## [1] 115.8571
mean(Orange$circumference) == Orange$circumference%>% mean()
## [1] TRUE
## Menghitung rata-rata Tree
Orange %>% group_by(Tree) %>% summarize(mean=mean(age))
## # A tibble: 5 × 2
##   Tree   mean
##   <ord> <dbl>
## 1 3      922.
## 2 1      922.
## 3 5      922.
## 4 2      922.
## 5 4      922.
Orange
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 1       118            30
##  2 1       484            58
##  3 1       664            87
##  4 1      1004           115
##  5 1      1231           120
##  6 1      1372           142
##  7 1      1582           145
##  8 2       118            33
##  9 2       484            69
## 10 2       664           111
## # ℹ 25 more rows
## Mengurutkan dari nilai terkecil
Orange %>% arrange(age) %>%
  print(n=10)
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 1       118            30
##  2 2       118            33
##  3 3       118            30
##  4 4       118            32
##  5 5       118            30
##  6 1       484            58
##  7 2       484            69
##  8 3       484            51
##  9 4       484            62
## 10 5       484            49
## # ℹ 25 more rows
## Mengurutkan dari nilai terbesar
Orange %>% arrange(desc(age))
## # A tibble: 35 × 3
##    Tree    age circumference
##    <ord> <dbl>         <dbl>
##  1 1      1582           145
##  2 2      1582           203
##  3 3      1582           140
##  4 4      1582           214
##  5 5      1582           177
##  6 1      1372           142
##  7 2      1372           203
##  8 3      1372           139
##  9 4      1372           209
## 10 5      1372           174
## # ℹ 25 more rows
#Filter
Orange %>% filter(Tree=="1")
## # A tibble: 7 × 3
##   Tree    age circumference
##   <ord> <dbl>         <dbl>
## 1 1       118            30
## 2 1       484            58
## 3 1       664            87
## 4 1      1004           115
## 5 1      1231           120
## 6 1      1372           142
## 7 1      1582           145
Orange %>% select(Tree,circumference)
## # A tibble: 35 × 2
##    Tree  circumference
##    <ord>         <dbl>
##  1 1                30
##  2 1                58
##  3 1                87
##  4 1               115
##  5 1               120
##  6 1               142
##  7 1               145
##  8 2                33
##  9 2                69
## 10 2               111
## # ℹ 25 more rows
Orange %>% select(Tree,age,-circumference)
## # A tibble: 35 × 2
##    Tree    age
##    <ord> <dbl>
##  1 1       118
##  2 1       484
##  3 1       664
##  4 1      1004
##  5 1      1231
##  6 1      1372
##  7 1      1582
##  8 2       118
##  9 2       484
## 10 2       664
## # ℹ 25 more rows
## Mutate bikin variabel baru
Orange %>% mutate(Total=age+circumference)
## # A tibble: 35 × 4
##    Tree    age circumference Total
##    <ord> <dbl>         <dbl> <dbl>
##  1 1       118            30   148
##  2 1       484            58   542
##  3 1       664            87   751
##  4 1      1004           115  1119
##  5 1      1231           120  1351
##  6 1      1372           142  1514
##  7 1      1582           145  1727
##  8 2       118            33   151
##  9 2       484            69   553
## 10 2       664           111   775
## # ℹ 25 more rows
NewOrange <- Orange %>% select(Tree,age, circumference) %>% mutate(Total=age+circumference)
NewOrange
## # A tibble: 35 × 4
##    Tree    age circumference Total
##    <ord> <dbl>         <dbl> <dbl>
##  1 1       118            30   148
##  2 1       484            58   542
##  3 1       664            87   751
##  4 1      1004           115  1119
##  5 1      1231           120  1351
##  6 1      1372           142  1514
##  7 1      1582           145  1727
##  8 2       118            33   151
##  9 2       484            69   553
## 10 2       664           111   775
## # ℹ 25 more rows