Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))
Lst$name
## [1] "Fred"
Lst$wife
## [1] "Mary"
x <- "name"; Lst[[x]]
## [1] "Fred"
#Constructing and modifying
Lst$name
## [1] "Fred"
Lst$wife
## [1] "Mary"
Lst$no.children
## [1] 3
Lst$child.ages
## [1] 4 7 9
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
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
# Buat data negara bagian dan ubah jadi faktor
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
"sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
"sa", "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
# Buat data pendapatan (incomes)
incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
59, 46, 58, 43)
# Potong pendapatan jd bbrp kategori
incomef <- factor(cut(incomes, breaks = 3))
accountants <- data.frame(home = statef, loot = incomes, shot = incomef)
accountants
## home loot shot
## 1 tas 60 (50,60]
## 2 sa 49 (40,50]
## 3 qld 40 (40,50]
## 4 nsw 61 (60,70]
## 5 nsw 64 (60,70]
## 6 nt 60 (50,60]
## 7 wa 59 (50,60]
## 8 wa 54 (50,60]
## 9 qld 62 (60,70]
## 10 vic 69 (60,70]
## 11 nsw 70 (60,70]
## 12 vic 42 (40,50]
## 13 qld 56 (50,60]
## 14 qld 61 (60,70]
## 15 sa 61 (60,70]
## 16 tas 61 (60,70]
## 17 sa 58 (50,60]
## 18 nt 51 (50,60]
## 19 wa 48 (40,50]
## 20 vic 65 (60,70]
## 21 qld 49 (40,50]
## 22 nsw 49 (40,50]
## 23 nsw 41 (40,50]
## 24 wa 48 (40,50]
## 25 sa 52 (50,60]
## 26 act 46 (40,50]
## 27 nsw 59 (50,60]
## 28 vic 46 (40,50]
## 29 vic 58 (50,60]
## 30 act 43 (40,50]
attach(accountants)
home
## [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa tas sa nt wa
## [20] vic qld nsw nsw wa sa act nsw vic vic act
## Levels: act nsw nt qld sa tas vic wa
loot
## [1] 60 49 40 61 64 60 59 54 62 69 70 42 56 61 61 61 58 51 48 65 49 49 41 48 52
## [26] 46 59 46 58 43
shot
## [1] (50,60] (40,50] (40,50] (60,70] (60,70] (50,60] (50,60] (50,60] (60,70]
## [10] (60,70] (60,70] (40,50] (50,60] (60,70] (60,70] (60,70] (50,60] (50,60]
## [19] (40,50] (60,70] (40,50] (40,50] (40,50] (40,50] (50,60] (40,50] (50,60]
## [28] (40,50] (50,60] (40,50]
## Levels: (40,50] (50,60] (60,70]
detach(accountants)
lentils <- data.frame(
u = c(1, 2, 3),
v = c(4, 5, 6),
w = c(7, 8, 9)
)
attach(lentils)
u <- v+w
lentils$u <- v+w
detach()
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
attach(lentils)
## The following object is masked _by_ .GlobalEnv:
##
## u
search()
## [1] ".GlobalEnv" "lentils" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
detach("lentils")
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
file_text_1 <- "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"
writeLines(file_text_1, "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
input_text <- "A 10 20
B 30 40
C 50 60"
writeLines(input_text, "input.dat")
inp <- scan("input.dat", list("", 0, 0))
label <- inp[[1]]
x <- inp[[2]]
y <- inp[[3]]
label
## [1] "A" "B" "C"
x
## [1] 10 30 50
y
## [1] 20 40 60
inp <- scan("input.dat", list(id = "", x = 0, y = 0))
label <- inp$id
x <- inp$x
y <- inp$y
inp$id
## [1] "A" "B" "C"
inp$x
## [1] 10 30 50
inp$y
## [1] 20 40 60
writeLines("1 2 3 4 5 6 7 8 9 10", "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
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
data(package = "datasets")
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
xold <- data.frame(
Nama = c("Ed", "Arra", "Martin"),
Nilai = c(80, 90, 75)
)
xnew <- edit(xold)
View(xold)
x_baru <- edit(data.frame())