October 18th, 2016
Objects in R:
Like vectores, but mixing different kinds of elements
> people <- list(c(60, 72, 57, 90, 95, 72), + c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91), + c("Ali", "Deniz", "Fatma", "Emre", "Volkan", "Onur"), + TRUE, + factor(c("M","F","F","M","M","M")))
Notice that elements can have different length
> people
[[1]] [1] 60 72 57 90 95 72 [[2]] [1] 1.75 1.80 1.65 1.90 1.74 1.91 [[3]] [1] "Ali" "Deniz" "Fatma" "Emre" "Volkan" "Onur" [[4]] [1] TRUE [[5]] [1] M F F M M M Levels: F M
> people[1:2]
[[1]] [1] 60 72 57 90 95 72 [[2]] [1] 1.75 1.80 1.65 1.90 1.74 1.91
This is a sublist (with one element):
> people[1]
[[1]] [1] 60 72 57 90 95 72
This is an element:
> people[[1]]
[1] 60 72 57 90 95 72
> people <- list(weight=c(60, 72, 57, 90, 95, 72), + height=c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91), + names=c("Ali", "Deniz", "Fatma", "Emre", "Volkan", "Onur"), + valid=TRUE, + gender=factor(c("M","F","F","M","M","M")))
How else can we assign names?
> people
$weight [1] 60 72 57 90 95 72 $height [1] 1.75 1.80 1.65 1.90 1.74 1.91 $names [1] "Ali" "Deniz" "Fatma" "Emre" "Volkan" "Onur" $valid [1] TRUE $gender [1] M F F M M M Levels: F M
> people[1:2]
$weight [1] 60 72 57 90 95 72 $height [1] 1.75 1.80 1.65 1.90 1.74 1.91
This is a sublist:
> people[1]
$weight [1] 60 72 57 90 95 72
This is an element:
> people[[1]]
[1] 60 72 57 90 95 72
> people[[1]]
[1] 60 72 57 90 95 72
> people[["weight"]]
[1] 60 72 57 90 95 72
> people$weight
[1] 60 72 57 90 95 72
Indices can also be used to change specifc parts of a list.
Try each of the following and explain the result:
> people$names <- toupper(people$names) > people$BMI <- people$weight/people$height^2 > people$valid <- NULL
Try these
> people[[2]] > people[2] > people[[2]][3] > people[2][3] > people[[1:3]] > people[1:3] > people[["weight"]] > people$weight > people["weight"]
> people[[2]]
[1] 1.75 1.80 1.65 1.90 1.74 1.91
> people[2]
$height [1] 1.75 1.80 1.65 1.90 1.74 1.91
> people[[2]][3]
[1] 1.65
> people[2][3]
$<NA> NULL
> people[[1:3]]
Error in people[[1:3]]: recursive indexing failed at level 2
> people[1:3]
$weight [1] 60 72 57 90 95 72 $height [1] 1.75 1.80 1.65 1.90 1.74 1.91 $names [1] "ALI" "DENIZ" "FATMA" "EMRE" "VOLKAN" "ONUR"
> people[["weight"]]
[1] 60 72 57 90 95 72
> people$weight
[1] 60 72 57 90 95 72
> people["weight"]
$weight [1] 60 72 57 90 95 72
If key <- "names"
,
What is the diference between the following?
people[[key]]
people[[names]]
people$key
people$names
Explain
Write a list with one element for each person, representing the name, weight, height and gender.
> ppl <- data.frame(weight=c(60, 72, 57, 90, 95, 72), + height=c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91), + names=c("Ali", "Deniz", "Fatma", "Emre", "Volkan", "Onur"), + gender=factor(c("M","F","F","M","M","M")))
> ppl
weight height names gender 1 60 1.75 Ali M 2 72 1.80 Deniz F 3 57 1.65 Fatma F 4 90 1.90 Emre M 5 95 1.74 Volkan M 6 72 1.91 Onur M
If df
is a data.frame, then df[[1]]
is a vector
matrix
columns have all the same typelist
the elements can have any size