6 Lists and data frames

# 6.1 Lists
Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))
Lst$name 
## [1] "Fred"
Lst$wife
## [1] "Mary"
Lst$child.ages[1]
## [1] 4
x <- "name"; Lst[[x]]
## [1] "Fred"
# 6.2 structing and modifying listsrix(1;4
Mat <- matrix(1:4,nrow = 2, ncol = 2)
Lst[5] <- list(matrix=Mat)
Lst
## $name
## [1] "Fred"
## 
## $wife
## [1] "Mary"
## 
## $no.children
## [1] 3
## 
## $child.ages
## [1] 4 7 9
## 
## [[5]]
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
# Concatenating lists
list.A <- list(angka = 1:3)
list.B <- list(huruf = c("a", "b", "c"))
list.C <- list(status = TRUE)

list.ABC <- c(list.A, list.B, list.C)
list.ABC
## $angka
## [1] 1 2 3
## 
## $huruf
## [1] "a" "b" "c"
## 
## $status
## [1] TRUE
# 6.3 & 6.3.1 Making data frames (Membuat Data Frame)
statef  <- factor(c("tas", "sa", "qld", "nsw", "sa"))
incomes <- c(60, 49, 40, 61, 64)
incomef <- factor(cut(incomes, breaks = 3))


accountants <- data.frame(
  home = statef, 
  loot = incomes, 
  shot = incomef
)


contoh_list <- list(x = 1:3, y = c("A", "B", "C"))
df_dari_list <- as.data.frame(contoh_list)
df_dari_list
##   x y
## 1 1 A
## 2 2 B
## 3 3 C
# 6.3.2 attach() and detach()
lentils <- data.frame(
  u = c(1, 2, 3),
  v = c(4, 5, 6),
  w = c(7, 8, 9)
)

attach(lentils)

u <- v + w
u        
## [1] 11 13 15
lentils  
##   u v w
## 1 1 4 7
## 2 2 5 8
## 3 3 6 9
lentils$u <- v + w

detach("lentils")

rm(u)

lentils
##    u v w
## 1 11 4 7
## 2 13 5 8
## 3 15 6 9
# 6.3.3 Working with data frames
data_proyek <- data.frame(
  panjang = c(10, 20, 30),
  lebar   = c(2, 4, 5)
)

attach(data_proyek)

data_proyek$luas <- panjang * lebar

detach(data_proyek)

data_proyek
##   panjang lebar luas
## 1      10     2   20
## 2      20     4   80
## 3      30     5  150
# 6.3.4 Attaching arbitrary lists
any.old.list <- list(
  nama  = "wawa",
  skor  = c(80, 90, 85),
  lulus = TRUE
)

attach(any.old.list)

nama
## [1] "wawa"
skor
## [1] 80 90 85
lulus
## [1] TRUE
detach("any.old.list")
# 6.3.5 Managing the search path
search()
## [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
## [4] "package:grDevices" "package:utils"     "package:datasets" 
## [7] "package:methods"   "Autoloads"         "package:base"
attach(lentils)

search()
##  [1] ".GlobalEnv"        "lentils"           "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"
ls(2)
## [1] "u" "v" "w"
detach("lentils")

search()
## [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
## [4] "package:grDevices" "package:utils"     "package:datasets" 
## [7] "package:methods"   "Autoloads"         "package:base"

7 Reading data from files

# 7.1 The read.table() function
# Contoh 1
cat("Price Floor Area Rooms Age Cent.heat
01 52.00 111.0 830 5 6.2 no
02 54.75 128.0 710 5 7.5 no
03 57.50 101.0 1000 5 4.2 no
04 57.50 131.0 690 6 8.8 no
05 59.75 93.0 900 5 1.9 yes\n", file = "houses.data")


HousePrice <- read.table("houses.data")
HousePrice
##    Price Floor Area Rooms Age Cent.heat
## 01 52.00   111  830     5 6.2        no
## 02 54.75   128  710     5 7.5        no
## 03 57.50   101 1000     5 4.2        no
## 04 57.50   131  690     6 8.8        no
## 05 59.75    93  900     5 1.9       yes
# Contoh 2
cat("Price Floor Area Rooms Age Cent.heat
52.00 111.0 830 5 6.2 no
54.75 128.0 710 5 7.5 no
57.50 101.0 1000 5 4.2 no
57.50 131.0 690 6 8.8 no
59.75 93.0 900 5 1.9 yes\n", file = "houses_no_label.data")

HousePrice2 <- read.table("houses_no_label.data", header = TRUE)
HousePrice2
##   Price Floor Area Rooms Age Cent.heat
## 1 52.00   111  830     5 6.2        no
## 2 54.75   128  710     5 7.5        no
## 3 57.50   101 1000     5 4.2        no
## 4 57.50   131  690     6 8.8        no
## 5 59.75    93  900     5 1.9       yes
# 7.2 The scan() function
cat("A 10 20
B 30 40
C 50 60\n", file = "input.dat")

inp <- scan("input.dat", list("", 0, 0))
label <- inp[[1]]
x <- inp[[2]]
y <- inp[[3]]


inp <- scan("input.dat", list(id = "", x = 0, y = 0))
label <- inp$id
x <- inp$x
y <- inp$y

cat("1 2 3 4 5 6 7 8 9 10\n", file = "light.dat")

X <- matrix(scan("light.dat", 0), ncol = 5, byrow = TRUE)
X
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
# 7.3 Accessing builtin datasets
data()

data(infert)
head(infert)
##   education age parity induced case spontaneous stratum pooled.stratum
## 1    0-5yrs  26      6       1    1           2       1              3
## 2    0-5yrs  42      1       1    1           0       2              1
## 3    0-5yrs  39      6       2    1           0       3              4
## 4    0-5yrs  34      4       2    1           0       4              2
## 5   6-11yrs  35      3       1    1           1       5             32
## 6   6-11yrs  36      4       2    1           1       6             36
# 7.3.1 Loading data from other R packages
data(package = "rpart")


data(Puromycin, package = "datasets")
head(Puromycin)
##   conc rate   state
## 1 0.02   76 treated
## 2 0.02   47 treated
## 3 0.06   97 treated
## 4 0.06  107 treated
## 5 0.11  123 treated
## 6 0.11  139 treated
# 7.4 Editing data
xold <- data.frame(
  nama = c("wawa", "afi", "arra"),
  skor = c(80,88,90)
)

xnew <- edit(xold)

x_baru <- edit(data.frame())