Each phrase in a program is imperative.
Involves nouns, verbs and adverbs
Today we will focus on nouns
The only verb we need today is assign <-
Nouns are names of objects.
They only exist as reference to objects.
The most simple objects in R are vectors
This order is important. Keep in mind
Also known as categorical variables.
They are used for discrete values, for example when there is no natural order
These are variables that you would never average
> x <- c(1,2,3) > y <- c(10,20)
These are two numeric vectors. We can concatenate them
> c(x, y, 5) [1] 1 2 3 10 20 5
Notice that we use <-
for assignment.
Logical Vectors
> c(TRUE, TRUE, FALSE, TRUE) [1] TRUE TRUE FALSE TRUE
We can also write c(T,T,F,T)
A comparison creates a logical vector
> weight > 25 [1] FALSE FALSE FALSE FALSE TRUE FALSE
Same idea. Concatenation
Each element must be between single or double quotes
> c("alpha", 'beta', "gamma") [1] "alpha" "beta" "gamma" > c('he said "yes"', "I don't know") [1] "he said \"yes\"" "I don't know"
Special characters are coded with two symbols:\"
, \\
, \n
, \t
Easy. Any character vector can be transformed into a factor
> 4:9 [1] 4 5 6 7 8 9 > seq(4,9) [1] 4 5 6 7 8 9 > seq(4,10,2) [1] 4 6 8 10 > seq(from=4, by=2, length=4) [1] 4 6 8 10
> rep(1,3) [1] 1 1 1 > rep(c(7,9,13), 3) [1] 7 9 13 7 9 13 7 9 13 > rep(c(7,9,13), 1:3) [1] 7 9 9 13 13 13
> rep(1:2,c(10,5)) [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 > rep(c(TRUE,FALSE),3) [1] TRUE FALSE TRUE FALSE TRUE FALSE > rep(c(TRUE,FALSE),c(3,3)) [1] TRUE TRUE TRUE FALSE FALSE FALSE
> c(NA,TRUE, FALSE) [1] NA TRUE FALSE > c(NA,1,2) [1] NA 1 2
> c(1, "tail") [1] "1" "tail" > c(TRUE, "tail") [1] "TRUE" "tail"
> c(2,TRUE, FALSE) [1] 2 1 0 > c(factor(c("a","b")),"c") [1] "1" "2" "c"
> weight <- c(Peter=60, John=72, Frank=57, Huey=90, Dewey=95, Louie=72) > weight Peter John Frank Huey Dewey Louie 60 72 57 90 95 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)
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
names(vector) is.null(names(weight))
> 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[2,] A B C 72 90 72 > M[,3] x y 95 72 > M[,2:3] B C x 57 95 y 90 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("Peter","John","Frank","Huey","Dewey", "Louie"), valid=TRUE, gender=factor(rep("M",6),levels=c("M","F")))
> 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] "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
> 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"), IMC=IMC, gender=factor(rep("H",6),levels=c("H","M")))
> ppl weight height names BMI gender 1 60 1.75 Peter 19.59184 M 2 72 1.80 John 22.22222 M 3 57 1.65 Frank 20.93664 M 4 90 1.90 Huey 24.93075 M 5 95 1.74 Dewey 31.37799 M 6 72 1.91 Louie 19.73630 M