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:

install.packages(“GGally”) install.packages(“psych”) install.packages(“readr”) install.packages(“dplyr”) install.packages(“corrplot”) install.packages(“summarytools”) install.packages(“gridExtra”) install.packages(“tidyverse”) install.packages(“ggplot2”) data(mtcars) df <- mtcars head(df)

summary(data) head(data) dplyr::glimpse(data) library(tidyverse) library(readr) library(ggplot2) data(mtcars) head(df) library(ggplot2)

install.packages(“rvest”) install.packages(“dplyr”) install.packages(“stringr”)

install.packages(“RSelenium”) install.packages(“dplyr”) install.packages(“rvest”) install.packages(“dplyr”) install.packages(“stringr”)

library(rvest) library(dplyr) library(stringr)

URL halaman Wikipedia

url <- “https://id.wikipedia.org/wiki/Daftar_kota_di_Indonesia_menurut_jumlah_penduduk

Baca halaman

web <- read_html(url)

Ambil tabel pertama yang sesuai

tabel <- web %>% html_elements(“table.wikitable”) %>% .[[1]] %>% html_table(fill = TRUE)

Bersihkan dan ubah nama kolom

colnames(tabel) <- c(“No”, “Kota”, “Provinsi”, “Penduduk_2024”, “Penduduk_2014”, “Perubahan”)

Bersihkan data dari karakter non-angka jika perlu

tabel <- tabel %>% mutate( Penduduk_2024 = as.numeric(str_replace_all(Penduduk_2024, “\D”, ““)), Penduduk_2014 = as.numeric(str_replace_all(Penduduk_2014,”\D”, ““)), Perubahan = as.numeric(str_replace_all(Perubahan,”[^\\d-]“,”“)) )

Tampilkan data

head(tabel)

Baca halaman

web <- read_html(url)

Ambil tabel pertama yang sesuai

tabel <- web %>% html_elements(“table.wikitable”) %>% .[[1]] %>% html_table(fill = TRUE)

Bersihkan dan ubah nama kolom

colnames(tabel) <- c(“No”, “Kota”, “Provinsi”, “Penduduk_2024”, “Penduduk_2014”, “Perubahan”)

Bersihkan data dari karakter non-angka jika perlu

tabel <- tabel %>% mutate( Penduduk_2024 = as.numeric(str_replace_all(Penduduk_2024, “\D”, ““)), Penduduk_2014 = as.numeric(str_replace_all(Penduduk_2014,”\D”, ““)), Perubahan = as.numeric(str_replace_all(Perubahan,”[^\\d-]“,”“)) )

Tampilkan data

head(tabel)

library(ggplot2)

Ambil 10 kota terpadat berdasarkan penduduk 2024

top10 <- tabel %>% arrange(desc(Penduduk_2024)) %>% slice(1:10)

Buat grafik batang

ggplot(top10, aes(x = reorder(Kota, Penduduk_2024), y = Penduduk_2024)) + geom_bar(stat = “identity”, fill = “steelblue”) + coord_flip() + labs( title = “10 Kota Terpadat di Indonesia (2024)”, x = “Kota”, y = “Jumlah Penduduk” ) + theme_minimal()