What is a vector?
What basic types of data does a vector handle?
How do we create a vector?
How do we assign a vector?
How do we put names to a vector?
How do we index a vector?
Basic types are
Character
Numeric
Factor
Logic
Describe each type
c(1,2,3)
c(TRUE, TRUE, FALSE, TRUE)
c("alpha", 'beta', "gamma")
weight > 25
factor(c("head","tail"))
4:9
seq(4,10,2)
seq(from=4, by=2, length=4)
rep(1,3)
rep(c(TRUE,FALSE),3)
rep(c(TRUE,FALSE),c(3,3))
c(NA,TRUE, FALSE)
How do we write " inside a character string?
How do we write a TAB?
How do we assign a value to a variable?
What characters can be used to name a variable?
Special characters are coded with two symbols: \"
, \\
, \n
, \t
.
We use <-
for assignment.
Variable names start with a letter, and are followd by a letter, number or dot.
Every element can have a name
> weight <- c(Peter=60, John=72, Frank=57, Huey=90, Dewey=95, Louie=72) > names(weight) [1] "Peter" "John" "Frank" "Huey" "Dewey" "Louie" > height <- c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91) > names(height) <- names(weight)
Notice that in some cases the command can spread multiple lines
v
we use v[i]
> weight[3] Frank 57 > weight[c(1,3,5)] Peter Frank Dewey 60 57 95 > weight[2:4]
> weight Peter John Frank Huey Dewey Louie 60 72 57 90 95 72 > weight[c(-1,-3,-5)] John Huey Louie 72 90 72
> weight>72 Peter John Frank Huey Dewey Louie FALSE FALSE FALSE TRUE TRUE FALSE > weight[weight>72] Huey Dewey 90 95
> weight[c("Peter","John","Frank")] Peter John Frank 60 72 57
a[1]
a["1"]
?> matrix(weight, nrow=2, ncol=3) [,1] [,2] [,3] [1,] 60 57 95 [2,] 72 90 72 > matrix(weight, nrow=2, ncol=3, byrow=T) [,1] [,2] [,3] [1,] 60 72 57 [2,] 90 95 72
> M <- matrix(weight, nrow=2, ncol=3) > dim(M) [1] 2 3
nrow(M)
y ncol(M)
> colnames(M) <- c("A","B","C") > rownames(M) <- c("x","y") > M A B C x 60 57 95 y 72 90 72
> A <- array(0, dim=c(2,3,2)) > A , , 1 [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 , , 2 [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0
M[1,1]
, M["x","A"]
> M[2,] A B C 72 90 72 > M[,3] x y 95 72
Notice that sometimes the answer is a vector, other times is a matrix
> M[,2:3] B C x 57 95 y 90 72
M <- outer(11:22, 11:17)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 121 132 143 154 165 176 187 [2,] 132 144 156 168 180 192 204 [3,] 143 156 169 182 195 208 221 [4,] 154 168 182 196 210 224 238 [5,] 165 180 195 210 225 240 255 [6,] 176 192 208 224 240 256 272 [7,] 187 204 221 238 255 272 289 [8,] 198 216 234 252 270 288 306 [9,] 209 228 247 266 285 304 323 [10,] 220 240 260 280 300 320 340 [11,] 231 252 273 294 315 336 357 [12,] 242 264 286 308 330 352 374
month.abb
> 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("Peter", "John", "Frank", "Huey", "Dewey", "Louie"), gender=factor(rep("M",6), levels=c("F","M")))
> ppl weight height names gender 1 60 1.75 Peter M 2 72 1.80 John M 3 57 1.65 Frank M 4 90 1.90 Huey M 5 95 1.74 Dewey M 6 72 1.91 Louie M
Look for the documentation of read.table()
Read the file birth.txt
people <- list( c(60,72,57,90,95, 72), c(1.75,1.80,1.65,1.90,1.74, 1.91), c("Peter", "John", "Frank", "Huey", "Dewey", "Louie"), TRUE, factor(rep("M",6), levels=c("M","F")))
[[1]] [1] 60 72 57 90 95 72 [[2]] [1] 1.75 1.80 1.65 1.90 1.74 1.91 [[3]] [1] "Peter" "John" "Frank" "Huey" "Dewey" "Louie" [[4]] [1] TRUE [[5]] [1] M M M M M M Levels: M F
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("Peter", "John", "Frank", "Huey", "Dewey", "Louie"), valid=TRUE, gender=factor(rep("M",6), levels=c("M","F")))
$weight [1] 60 72 57 90 95 72 $height [1] 1.75 1.80 1.65 1.90 1.74 1.91 $names [1] "Peter" "John" "Frank" "Huey" "Dewey" "Louie" $valid [1] TRUE $gender [1] M M M 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
> people[1] $weight [1] 60 72 57 90 95 72
> people[[1]] [1] 60 72 57 90 95 72
people[["weight"]]
people$weight
Try these
people[[2]] people[2] people[[2]][3] people[2][3] people[[1:3]] people[1:3] people[["weight"]] people$weight people["weight"]
Write a list with one element for each person, representing the name, weight, height and gender.