library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── 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)
library(dplyr)
data(airquality)
kualitas_udara <- tibble::as.tibble(airquality)
## 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.
class(kualitas_udara)
## [1] "tbl_df" "tbl" "data.frame"
view(kualitas_udara)
head(kualitas_udara)
## # A tibble: 6 × 6
## Ozone Solar.R Wind Temp Month Day
## <int> <int> <dbl> <int> <int> <int>
## 1 41 190 7.4 67 5 1
## 2 36 118 8 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
glimpse(kualitas_udara)
## Rows: 153
## Columns: 6
## $ Ozone <int> 41, 36, 12, 18, NA, 28, 23, 19, 8, NA, 7, 16, 11, 14, 18, 14, …
## $ Solar.R <int> 190, 118, 149, 313, NA, NA, 299, 99, 19, 194, NA, 256, 290, 27…
## $ Wind <dbl> 7.4, 8.0, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1, 8.6, 6.9, 9…
## $ Temp <int> 67, 72, 74, 62, 56, 66, 65, 59, 61, 69, 74, 69, 66, 68, 58, 64…
## $ Month <int> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
## $ Day <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,…
#Menghapus nilai NA
kualitas_udara<- kualitas_udara %>%
filter_all(all_vars(!is.na(.)))
kualitas_udara
## # A tibble: 111 × 6
## Ozone Solar.R Wind Temp Month Day
## <int> <int> <dbl> <int> <int> <int>
## 1 41 190 7.4 67 5 1
## 2 36 118 8 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 23 299 8.6 65 5 7
## 6 19 99 13.8 59 5 8
## 7 8 19 20.1 61 5 9
## 8 16 256 9.7 69 5 12
## 9 11 290 9.2 66 5 13
## 10 14 274 10.9 68 5 14
## # ℹ 101 more rows
kualitas_udara$Ozone <- gsub(",", "", kualitas_udara$Ozone)
kualitas_udara
## # A tibble: 111 × 6
## Ozone Solar.R Wind Temp Month Day
## <chr> <int> <dbl> <int> <int> <int>
## 1 41 190 7.4 67 5 1
## 2 36 118 8 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 23 299 8.6 65 5 7
## 6 19 99 13.8 59 5 8
## 7 8 19 20.1 61 5 9
## 8 16 256 9.7 69 5 12
## 9 11 290 9.2 66 5 13
## 10 14 274 10.9 68 5 14
## # ℹ 101 more rows
#hitung
#Menghitung Rata-rata
rata2=mean(kualitas_udara$Solar.R)
#summarize(ringkas data)
## HITUNG RATA-RATA SOLAR PER BULAN
kualitas_udara %>% group_by(Month) %>%
summarize(Mean_solar=mean(Solar.R))
## # A tibble: 5 × 2
## Month Mean_solar
## <int> <dbl>
## 1 5 182.
## 2 6 184.
## 3 7 216.
## 4 8 173.
## 5 9 168.
## MENGURUTKAN DATA BERDASARKAN TEMPERATUR
print(kualitas_udara) %>%
arrange(Temp)
## # A tibble: 111 × 6
## Ozone Solar.R Wind Temp Month Day
## <chr> <int> <dbl> <int> <int> <int>
## 1 41 190 7.4 67 5 1
## 2 36 118 8 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 23 299 8.6 65 5 7
## 6 19 99 13.8 59 5 8
## 7 8 19 20.1 61 5 9
## 8 16 256 9.7 69 5 12
## 9 11 290 9.2 66 5 13
## 10 14 274 10.9 68 5 14
## # ℹ 101 more rows
## # A tibble: 111 × 6
## Ozone Solar.R Wind Temp Month Day
## <chr> <int> <dbl> <int> <int> <int>
## 1 6 78 18.4 57 5 18
## 2 18 65 13.2 58 5 15
## 3 19 99 13.8 59 5 8
## 4 1 8 9.7 59 5 21
## 5 8 19 20.1 61 5 9
## 6 4 25 9.7 61 5 23
## 7 32 92 12 61 5 24
## 8 18 313 11.5 62 5 4
## 9 11 44 9.7 62 5 20
## 10 14 20 16.6 63 9 25
## # ℹ 101 more rows
## Mengurutkan data besar -> kecil
kualitas_udara %>% arrange(desc(Temp))
## # A tibble: 111 × 6
## Ozone Solar.R Wind Temp Month Day
## <chr> <int> <dbl> <int> <int> <int>
## 1 76 203 9.7 97 8 28
## 2 84 237 6.3 96 8 30
## 3 118 225 2.3 94 8 29
## 4 85 188 6.3 94 8 31
## 5 73 183 2.8 93 9 3
## 6 91 189 4.6 93 9 4
## 7 97 267 6.3 92 7 8
## 8 97 272 5.7 92 7 9
## 9 78 197 5.1 92 9 2
## 10 96 167 6.9 91 9 1
## # ℹ 101 more rows
#AMBIL DATA PADA BULAN 7
kualitas_udara %>% filter(Month=="7")
## # A tibble: 26 × 6
## Ozone Solar.R Wind Temp Month Day
## <chr> <int> <dbl> <int> <int> <int>
## 1 135 269 4.1 84 7 1
## 2 49 248 9.2 85 7 2
## 3 32 236 9.2 81 7 3
## 4 64 175 4.6 83 7 5
## 5 40 314 10.9 83 7 6
## 6 77 276 5.1 88 7 7
## 7 97 267 6.3 92 7 8
## 8 97 272 5.7 92 7 9
## 9 85 175 7.4 89 7 10
## 10 10 264 14.3 73 7 12
## # ℹ 16 more rows
#AMBIL DATA PADA KOLOM TERTENTU
kualitas_udara %>%
select(Ozone,Solar.R)
## # A tibble: 111 × 2
## Ozone Solar.R
## <chr> <int>
## 1 41 190
## 2 36 118
## 3 12 149
## 4 18 313
## 5 23 299
## 6 19 99
## 7 8 19
## 8 16 256
## 9 11 290
## 10 14 274
## # ℹ 101 more rows
#AMBIL DATA DENGAN HAPUS KOLOM TERTENTU
kualitas_udara%>%
select(-Day,-Wind)
## # A tibble: 111 × 4
## Ozone Solar.R Temp Month
## <chr> <int> <int> <int>
## 1 41 190 67 5
## 2 36 118 72 5
## 3 12 149 74 5
## 4 18 313 62 5
## 5 23 299 65 5
## 6 19 99 59 5
## 7 8 19 61 5
## 8 16 256 69 5
## 9 11 290 66 5
## 10 14 274 68 5
## # ℹ 101 more rows
#MUTATE
kualitas_udara %>% mutate(Hari = Month *12 + Day)
## # A tibble: 111 × 7
## Ozone Solar.R Wind Temp Month Day Hari
## <chr> <int> <dbl> <int> <int> <int> <dbl>
## 1 41 190 7.4 67 5 1 61
## 2 36 118 8 72 5 2 62
## 3 12 149 12.6 74 5 3 63
## 4 18 313 11.5 62 5 4 64
## 5 23 299 8.6 65 5 7 67
## 6 19 99 13.8 59 5 8 68
## 7 8 19 20.1 61 5 9 69
## 8 16 256 9.7 69 5 12 72
## 9 11 290 9.2 66 5 13 73
## 10 14 274 10.9 68 5 14 74
## # ℹ 101 more rows
kualitas_udara_baru <- kualitas_udara%>% select(-Month,-Day)
kualitas_udara_baru
## # A tibble: 111 × 4
## Ozone Solar.R Wind Temp
## <chr> <int> <dbl> <int>
## 1 41 190 7.4 67
## 2 36 118 8 72
## 3 12 149 12.6 74
## 4 18 313 11.5 62
## 5 23 299 8.6 65
## 6 19 99 13.8 59
## 7 8 19 20.1 61
## 8 16 256 9.7 69
## 9 11 290 9.2 66
## 10 14 274 10.9 68
## # ℹ 101 more rows
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
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.