library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'tidyr' was built under R version 4.5.3
## Warning: package 'purrr' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## Warning: package 'stringr' was built under R version 4.5.3
## Warning: package 'forcats' was built under R version 4.5.3
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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_packages()
## [1] "broom" "conflicted" "cli" "dbplyr"
## [5] "dplyr" "dtplyr" "forcats" "ggplot2"
## [9] "googledrive" "googlesheets4" "haven" "hms"
## [13] "httr" "jsonlite" "lubridate" "magrittr"
## [17] "modelr" "pillar" "purrr" "ragg"
## [21] "readr" "readxl" "reprex" "rlang"
## [25] "rstudioapi" "rvest" "stringr" "tibble"
## [29] "tidyr" "xml2" "tidyverse"
You can also embed plots, for example:
tibble_karyawan <- tibble(
`nama karyawan` = c("Andi", "Budi", "Cici"),
gaji = c(5000000, 6000000, 5500000),
tunjangan = list(c(100, 200), 150, c(150, 250, 50))
)
tibble_karyawan
## # A tibble: 3 × 3
## `nama karyawan` gaji tunjangan
## <chr> <dbl> <list>
## 1 Andi 5000000 <dbl [2]>
## 2 Budi 6000000 <dbl [1]>
## 3 Cici 5500000 <dbl [3]>
tibble_karyawan$gaji
## [1] 5000000 6000000 5500000
library(nycflights13)
## Warning: package 'nycflights13' was built under R version 4.5.3
flights
## # A tibble: 336,776 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # ℹ 336,766 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
df_flights <- as.data.frame(flights)
# menampilkan data teratas
head(df_flights)
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## arr_delay carrier flight tailnum origin dest air_time distance hour minute
## 1 11 UA 1545 N14228 EWR IAH 227 1400 5 15
## 2 20 UA 1714 N24211 LGA IAH 227 1416 5 29
## 3 33 AA 1141 N619AA JFK MIA 160 1089 5 40
## 4 -18 B6 725 N804JB JFK BQN 183 1576 5 45
## 5 -25 DL 461 N668DN LGA ATL 116 762 6 0
## 6 12 UA 1696 N39463 EWR ORD 150 719 5 58
## time_hour
## 1 2013-01-01 05:00:00
## 2 2013-01-01 05:00:00
## 3 2013-01-01 05:00:00
## 4 2013-01-01 05:00:00
## 5 2013-01-01 06:00:00
## 6 2013-01-01 05:00:00
head(df_flights$y)
## [1] 2013 2013 2013 2013 2013 2013
df_people <- data.frame(
id = c(1, 2, 3, 4, 5),
name = c("Adam", "Eva", "Miki", "Yola", "Jack"),
age = c(46, 48, 21, 19, 17),
gender = c("male", rep("female", 3), "male"),
drives = c(TRUE, TRUE, FALSE, TRUE, FALSE)
)
library(tibble)
tibble_people <- as_tibble(df_people)
tibble_people
## # A tibble: 5 × 5
## id name age gender drives
## <dbl> <chr> <dbl> <chr> <lgl>
## 1 1 Adam 46 male TRUE
## 2 2 Eva 48 female TRUE
## 3 3 Miki 21 female FALSE
## 4 4 Yola 19 female TRUE
## 5 5 Jack 17 male FALSE
summary(tibble_people)
## id name age gender
## Min. :1 Length:5 Min. :17.0 Length:5
## 1st Qu.:2 Class :character 1st Qu.:19.0 Class :character
## Median :3 Mode :character Median :21.0 Mode :character
## Mean :3 Mean :30.2
## 3rd Qu.:4 3rd Qu.:46.0
## Max. :5 Max. :48.0
## drives
## Mode :logical
## FALSE:2
## TRUE :3
##
##
##
glimpse(tibble_people)
## Rows: 5
## Columns: 5
## $ id <dbl> 1, 2, 3, 4, 5
## $ name <chr> "Adam", "Eva", "Miki", "Yola", "Jack"
## $ age <dbl> 46, 48, 21, 19, 17
## $ gender <chr> "male", "female", "female", "female", "male"
## $ drives <lgl> TRUE, TRUE, FALSE, TRUE, FALSE
tibble_people$gender <- factor(tibble_people$gender)
summary(tibble_people)
## id name age gender drives
## Min. :1 Length:5 Min. :17.0 female:3 Mode :logical
## 1st Qu.:2 Class :character 1st Qu.:19.0 male :2 FALSE:2
## Median :3 Mode :character Median :21.0 TRUE :3
## Mean :3 Mean :30.2
## 3rd Qu.:4 3rd Qu.:46.0
## Max. :5 Max. :48.0
tibble_people[ ,2]
## # A tibble: 5 × 1
## name
## <chr>
## 1 Adam
## 2 Eva
## 3 Miki
## 4 Yola
## 5 Jack
tibble_people[tibble_people$age < 30, ]
## # A tibble: 3 × 5
## id name age gender drives
## <dbl> <chr> <dbl> <fct> <lgl>
## 1 3 Miki 21 female FALSE
## 2 4 Yola 19 female TRUE
## 3 5 Jack 17 male FALSE
penjualankoma <- read_csv("C:/Users/ASUS/Downloads/penjualan.csv")
## Rows: 5 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): ID, Nama, Gender
## dbl (1): Usia
## num (1): Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualankoma
## # A tibble: 5 × 5
## ID Nama Usia Gender Penjualan
## <chr> <chr> <dbl> <chr> <dbl>
## 1 321A Eiden 35 Laki-laki 1125600
## 2 44B Olivia 28 Perempuan 987000
## 3 984C Brandon 25 Laki-laki 2134000
## 4 653D Wendy 30 Perempuan 756000
## 5 178E Loki 26 Laki-laki 1698000
penjualantitikoma <- read_csv2("C:/Users/ASUS/Downloads/penjualan.csv")
## ℹ Using "','" as decimal and "'.'" as grouping mark. Use `read_delim()` for more control.
## Rows: 5 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ";"
## chr (1): ID,Nama,Usia,Gender,Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualantitikoma
## # A tibble: 5 × 1
## `ID,Nama,Usia,Gender,Penjualan`
## <chr>
## 1 "321A,Eiden,35,Laki-laki,1125600"
## 2 "44B,Olivia,28,Perempuan,\"987,000\""
## 3 "984C,Brandon,25,Laki-laki,2134000"
## 4 "653D,Wendy,30,Perempuan,756000"
## 5 "178E,Loki,26,Laki-laki,1698000"
penjualantxtab <- read_tsv("C:/Users/ASUS/Downloads/penjualan.csv")
## Rows: 5 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): ID,Nama,Usia,Gender,Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualantxtab
## # A tibble: 5 × 1
## `ID,Nama,Usia,Gender,Penjualan`
## <chr>
## 1 "321A,Eiden,35,Laki-laki,1125600"
## 2 "44B,Olivia,28,Perempuan,\"987,000\""
## 3 "984C,Brandon,25,Laki-laki,2134000"
## 4 "653D,Wendy,30,Perempuan,756000"
## 5 "178E,Loki,26,Laki-laki,1698000"
penjualantxtsep <- read_delim("C:/Users/ASUS/Downloads/penjualan.csv", delim='|')
## Rows: 5 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "|"
## chr (1): ID,Nama,Usia,Gender,Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualantxtsep
## # A tibble: 5 × 1
## `ID,Nama,Usia,Gender,Penjualan`
## <chr>
## 1 "321A,Eiden,35,Laki-laki,1125600"
## 2 "44B,Olivia,28,Perempuan,\"987,000\""
## 3 "984C,Brandon,25,Laki-laki,2134000"
## 4 "653D,Wendy,30,Perempuan,756000"
## 5 "178E,Loki,26,Laki-laki,1698000"
penjualanreadelimkoma <- read_delim("C:/Users/ASUS/Downloads/penjualan.csv", delim=',')
## Rows: 5 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): ID, Nama, Gender
## dbl (1): Usia
## num (1): Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualanreadelimkoma
## # A tibble: 5 × 5
## ID Nama Usia Gender Penjualan
## <chr> <chr> <dbl> <chr> <dbl>
## 1 321A Eiden 35 Laki-laki 1125600
## 2 44B Olivia 28 Perempuan 987000
## 3 984C Brandon 25 Laki-laki 2134000
## 4 653D Wendy 30 Perempuan 756000
## 5 178E Loki 26 Laki-laki 1698000
penjualanreadelimtab <- read_delim("C:/Users/ASUS/Downloads/penjualan.csv", delim='\t')
## Rows: 5 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): ID,Nama,Usia,Gender,Penjualan
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
penjualanreadelimtab
## # A tibble: 5 × 1
## `ID,Nama,Usia,Gender,Penjualan`
## <chr>
## 1 "321A,Eiden,35,Laki-laki,1125600"
## 2 "44B,Olivia,28,Perempuan,\"987,000\""
## 3 "984C,Brandon,25,Laki-laki,2134000"
## 4 "653D,Wendy,30,Perempuan,756000"
## 5 "178E,Loki,26,Laki-laki,1698000"
path_tujuan <- “C:/Users/ASUS/Downloads/”
write_csv(tibble_karyawan, paste0(path_tujuan, ‘tibble_karyawancobakoma.csv’))
write_csv2(tibble_karyawan, paste0(path_tujuan, ‘tibble_karyawancobatitikkoma.csv’))
write_delim(tibble_karyawan, paste0(path_tujuan, ‘tibble_karyawancobaspasi.txt’), delim=’ ’)
write_tsv(tibble_karyawan, paste0(path_tujuan, ‘tibble_karyawancobatab.txt’))