Pada bagian ini saya belajar tentang Vectors.

library(tidyverse)

Vectors Basics

Ada dua jenis vectors:

  1. Atomic vectors, yaitu logical, integer, double, character, complex, and, raw.

  2. Lists

Type Vectors


Setiap vector mempunyai dua keterangan, yaitu:

typeof(letters)
## [1] "character"
x1<-c(1,2,3,4)
typeof(x1)
## [1] "double"
x2<-list("a","c",1:9)
typeof(x2)
## [1] "list"
length(x1)
## [1] 4
length(x2)
## [1] 3

There are four important types of augmented vector:


Important Types of Atomic Vector

Ada empat type yang sangat penting pada atomic vectors, yaitu logical, integer, double, and character.

Logical

Singkatnya pada type logical hanya ada tiga value, yaitu TRUE, FALSE, dan NA.

Numeric

Ada dua type numeric yaitu double dan integer. Apabila ingin membuat data bertipe integer tambahkan “L” setelah angka.

typeof(1)
## [1] "double"
typeof(1L)
## [1] "integer"

Ada dua perbedaan pada integer dan double.

x <- sqrt(2) ^ 2
x
## [1] 2
x - 2
## [1] 4.440892e-16

Character

Value dari Type Character adalah string.


Using Atomic Vectors

Coercion

Ada dua cara mengubah type o vector ke type yang lain.

Scalars and Recycling Rules

Apabila kita menjumlahkan dua vektor dengan panjang berbeda, maka vektor yang panjangnya lebih kecil akan diulang sampai sama dengan vektor yang lebih panjang. Contoh

sample(10) + 100
##  [1] 102 105 109 101 110 103 108 104 106 107
1:10 + 1:2
##  [1]  2  4  4  6  6  8  8 10 10 12
1:10 + 1:3
## Warning in 1:10 + 1:3: longer object length is not a multiple of shorter object
## length
##  [1]  2  4  6  5  7  9  8 10 12 11

Naming Vectors

Bisa menggunakan c().

c(x = 1, y = 2, z = 4)
## x y z 
## 1 2 4

Bisa menggunakan set_names()

set_names(1:3, c("a", "b", "c"))
## a b c 
## 1 2 3

Subsetting

Menggunakan [].

x <- c("one", "two", "three", "four", "five")
x[c(3, 2, 5)]
## [1] "three" "two"   "five"
x[c(1, 1, 5, 5, 5, 2)]
## [1] "one"  "one"  "five" "five" "five" "two"
x[c(-1, -3, -5)]
## [1] "two"  "four"

Pemgguaan kode dibawah akan error.

x[c(1, -1)]

x[0]

x <- c(10, 3, NA, 5, 8, 1, NA)

x[!is.na(x)]
## [1] 10  3  5  8  1
x <- c(abc = 1, def = 2, xyz = 5)
x[c("xyz", "def")]
## xyz def 
##   5   2

Recursive Vectors (Lists)

List merupakan sesuatu yang ebih kompleks dari atomic vectors karena pada list berisi satu atau lebih jenis atomic vector dan dapat berisi list lainnya.

Contoh:

x <- list(a = 1, b = 2, c = 3)
str(x)
## List of 3
##  $ a: num 1
##  $ b: num 2
##  $ c: num 3
y <- list("a", 1L, 1.5, TRUE)
str(y)
## List of 4
##  $ : chr "a"
##  $ : int 1
##  $ : num 1.5
##  $ : logi TRUE
z <- list(list(1, 2), list(3, 4))
str(z)
## List of 2
##  $ :List of 2
##   ..$ : num 1
##   ..$ : num 2
##  $ :List of 2
##   ..$ : num 3
##   ..$ : num 4

Subseting List

a <- list(a = 1:3, b = "a string", c = pi, d = list(-1, -5))
a
## $a
## [1] 1 2 3
## 
## $b
## [1] "a string"
## 
## $c
## [1] 3.141593
## 
## $d
## $d[[1]]
## [1] -1
## 
## $d[[2]]
## [1] -5
# Menggunakan [], mengambil sublist, hasil masih berupa list
str(a[1:2])
## List of 2
##  $ a: int [1:3] 1 2 3
##  $ b: chr "a string"
str(a[4])
## List of 1
##  $ d:List of 2
##   ..$ : num -1
##   ..$ : num -5
# menggunakan [[]], mengambil single component, hasil berupa atomic vector.
str(a[[1]])
##  int [1:3] 1 2 3
str(a[[2]])
##  chr "a string"
str(a[[4]][[1]])
##  num -1
# Menggunakan $ hampir mirip sama [[]]
str(a$a)
##  int [1:3] 1 2 3

Augmented Vectors


Factors

Factor sendiri dibangun dari integer dan mempunyai tingkatan.

x <- factor(c("ab", "cd", "ab"), levels = c("ab", "cd", "ef"))
typeof(x)
## [1] "integer"
attributes(x)
## $levels
## [1] "ab" "cd" "ef"
## 
## $class
## [1] "factor"

Dates and Times

Dates pada R adalah vektor numerik yang menggambarkan bilangan hari dari 1 January 1970.

x <- as.Date("1971-01-01")
unclass(x)
## [1] 365
typeof(x)
## [1] "double"
attributes(x)
## $class
## [1] "Date"

Dates-times adalah vektor numerik dengan class POSIXct(saya blm paham juga tentang POSIXct, katanya mah waktu kalender), yang menggambarkan bilangan detik dari 1 January 1970.

x <- lubridate::ymd_hm("1970-01-01 03:00")
unclass(x)
## [1] 10800
## attr(,"tzone")
## [1] "UTC"
typeof(x)
## [1] "double"
attributes(x)
## $class
## [1] "POSIXct" "POSIXt" 
## 
## $tzone
## [1] "UTC"
y <- as.POSIXlt(x)
typeof(y)
## [1] "list"
attributes(y)
## $names
## [1] "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"
## 
## $class
## [1] "POSIXlt" "POSIXt" 
## 
## $tzone
## [1] "UTC"

Tibbles

Tibbles adalah Lists

tb <- tibble::tibble(x = 1:5, y = 5:1)
typeof(tb)
## [1] "list"
attributes(tb)
## $names
## [1] "x" "y"
## 
## $row.names
## [1] 1 2 3 4 5
## 
## $class
## [1] "tbl_df"     "tbl"        "data.frame"
df <- data.frame(x = 1:5, y = 5:1)
typeof(df)
## [1] "list"
attributes(df)
## $names
## [1] "x" "y"
## 
## $class
## [1] "data.frame"
## 
## $row.names
## [1] 1 2 3 4 5

SEMOGA BERMANFAAT