Vectors and Vectorized Calculations >1s(0 character(0) the workspace is empty >x=1:5 we assign the vector (1,2,3,4,5)to the object name x y=c(5,4,3,2,1)#c for concatenate x typing the object name displays the object content [1]12345 >y [1]54321 >1s(0 [1]"x""y" now the workspace contains the objects x and y >X^2 [1]1491625 the square operation is vectorized y+x [1]66666 the summation is elementwise,vectorized x"y same here [1]11627165 10
Vectors and Vectorized Calculations > ls() character(0) # the workspace is empty > x=1:5 # we assign the vector (1,2,3,4,5) to the object name x > y=c(5,4,3,2,1) # c for concatenate > x # typing the object name displays the object content [1] 1 2 3 4 5 > y [1] 5 4 3 2 1 > ls() [1] "x" "y" # now the workspace contains the objects x and y > xˆ2 [1] 1 4 9 16 25 # the square operation is vectorized > y+x [1] 6 6 6 6 6 # the summation is elementwise, vectorized > xˆy # same here [1] 1 16 27 16 5 10
Functions Creating Vectors >1:10 [1]12345678910 seq(-4,4,1)#sequence from -4 to in increments of 1 [1]-4-3-2-101234 c(1,2,4,6,7)#concatenation of 1,2,4,6,7 [1]12467 rep(1,10)#vector of 10 repeat 1's [1]1111111111 rep(c(1,2),5)#vector of 5 repeat c(1,2) [1]1212121212 rev(1:5)#reverse the vector 1:5 [1]54321 t(1:5)#transpose the column vector to a row vector (1 row matrix) [,1][,2][,3][,4][,5] [1,]12345 11
Functions Creating Vectors > 1:10 [1] 1 2 3 4 5 6 7 8 9 10 > seq(-4,4,1) # sequence from -4 to in increments of 1 [1] -4 -3 -2 -1 0 1 2 3 4 > c(1,2,4,6,7) # concatenation of 1,2,4,6,7 [1] 1 2 4 6 7 > rep(1,10) # vector of 10 repeat 1’s [1] 1 1 1 1 1 1 1 1 1 1 > rep(c(1,2),5) # vector of 5 repeat c(1, 2) [1] 1 2 1 2 1 2 1 2 1 2 > rev(1:5) # reverse the vector 1:5 [1] 5 4 3 2 1 > t(1:5) # transpose the column vector to a row vector (1 row matrix) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 11
Functions of Vectors mean(y)#mean is a system function,called with parentheses [1]3 typing mean without (..would display the function body median(x) [1]3 sum(x) [1]15 var(x) [1]2.5 sort(y) [1]12345 12
Functions of Vectors > mean(y) # mean is a system function, called with parentheses [1] 3 # typing mean without (..) would display the function body > median(x) [1] 3 > sum(x) [1] 15 > var(x) [1] 2.5 > sort(y) [1] 1 2 3 4 5 12
Data Modes We saw that vectors can be made up of numbers or numeric mode elements. We can also use character or logic data in vectors x=c("abc","ABC","xyzXYZ")#example of a character vector >X [1]"abc""ABC""xyzXYZ" >Y=c(TRUE,T,FALSE,F) example of a logic vector >Y [1]TRUE TRUE FALSEFALSE T and TRUE are equivalent,same with F and FALSE Note that we use no quotes on T,TRUE,F and FALSE 13
Data Modes We saw that vectors can be made up of numbers or numeric mode elements. We can also use character or logic data in vectors > x=c("abc","ABC","xyzXYZ") # example of a character vector > x [1] "abc" "ABC" "xyzXYZ" >Y=c(TRUE,T,FALSE,F) # example of a logic vector >Y [1] TRUE TRUE FALSE FALSE T and TRUE are equivalent, same with F and FALSE Note that we use no quotes on T, TRUE, F and FALSE 13
Logical Operators symbol function symbol function less than && logical AND > greater than 11 logical OR <= less than or equal to logical NOT >= greater than or equal to 三三 equal to != not equal to >1:5<=3 [1] TRUE TRUE TRUE FALSE FALSE >"abc"<"a" logical operations work on character data as well [1]FALSE "abc"<"abd"#lexicographical ordering (1]TRUE >1<"a" [1]TRUE Note the mode coercion-next slide 14
Logical Operators symbol function symbol function < less than && logical AND > greater than || logical OR <= less than or equal to ! logical NOT >= greater than or equal to == equal to != not equal to > 1:5<=3 [1] TRUE TRUE TRUE FALSE FALSE > "abc"<"a" # logical operations work on character data as well [1] FALSE > "abc"<"abd" # lexicographical ordering [1] TRUE > 1<"a" [1] TRUE Note the mode coercion −→ next slide 14