library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'dplyr' 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.4     ✔ 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(dplyr)
library(datasets)
data(Orange)
Orange <- tibble::as_tibble(Orange)

##Menampilkan kelas (tipe data) dari Orange
class(Orange)
## [1] "tbl_df"     "tbl"        "data.frame"
##Menampilkan data frame atau dataset Orange
View(Orange)

##Menampilkan ringkasan dari struktur dataset Orange
##Head menampilkan 6 baris data awal dan glimpse menampilkan informasi lebih terperinci termasuk informasi tiap kolom, tipe data serta beberapa baris awal
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
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…
##Menghitung rata-rata umur (age) pohon 
mean(Orange$age)
## [1] 922.1429
##Memeriksa apakah rata-rata umur (age) pohon dari dataset Orange sama ketika dihitung langsung dengan mean(Orange$age) dan ketika menggunakan (%>%) dari paket dplyr 
mean(Orange$age) == Orange$age %>% mean()
## [1] TRUE
##Memasukkan data
x <- c(109, 359, 163, 896, 515, 142, 117, 329, 407)
x
## [1] 109 359 163 896 515 142 117 329 407
##Melogaritmakan, mendiferensialkan, mengeksponensialkan kemudian membulatkan 1 angka dibelakang koma
round(exp(diff(log(x))),1)
## [1] 3.3 0.5 5.5 0.6 0.3 0.8 2.8 1.2
x %>% log () %>%
  diff () %>%
  exp () %>%
  round (1)
## [1] 3.3 0.5 5.5 0.6 0.3 0.8 2.8 1.2
#Summarize
##Menghitung rata-rata umur (age) dari setiap pohon
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.
##Mengurutkkan berdasarkan peubah age dengan ascending
Orange %>% arrange(age)
## # 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
##Mengurutkkan berdasarkan peubah age dengan descending
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
##Menampilkan data pohon 5
Orange %>% filter(Tree=="5")
## # A tibble: 7 × 3
##   Tree    age circumference
##   <ord> <dbl>         <dbl>
## 1 5       118            30
## 2 5       484            49
## 3 5       664            81
## 4 5      1004           125
## 5 5      1231           142
## 6 5      1372           174
## 7 5      1582           177
##Menampilkan data pohon dan lingkar batang (circumference)
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
##Menampilkan data yang bukan lingkar batang (circumference)
Orange %>% select(-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 (Menambah Peubah Baru)
##Menghitung rasio lingkar batang (circumference) dan age
Orange %>% mutate(Ratio=Orange$circumference / Orange$age)
## # A tibble: 35 × 4
##    Tree    age circumference  Ratio
##    <ord> <dbl>         <dbl>  <dbl>
##  1 1       118            30 0.254 
##  2 1       484            58 0.120 
##  3 1       664            87 0.131 
##  4 1      1004           115 0.115 
##  5 1      1231           120 0.0975
##  6 1      1372           142 0.103 
##  7 1      1582           145 0.0917
##  8 2       118            33 0.280 
##  9 2       484            69 0.143 
## 10 2       664           111 0.167 
## # ℹ 25 more rows
##Menghilangkan peubah Tree, kemudian memasukkan peubah ratio
Orange <- Orange %>% select(-Tree) %>% mutate(Ratio=Orange$circumference / Orange$age)
Orange
## # A tibble: 35 × 3
##      age circumference  Ratio
##    <dbl>         <dbl>  <dbl>
##  1   118            30 0.254 
##  2   484            58 0.120 
##  3   664            87 0.131 
##  4  1004           115 0.115 
##  5  1231           120 0.0975
##  6  1372           142 0.103 
##  7  1582           145 0.0917
##  8   118            33 0.280 
##  9   484            69 0.143 
## 10   664           111 0.167 
## # ℹ 25 more rows

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.