Mode Coercion Mixed mode elements in expressions are coerced to a lowest common denominator mode mode order:logical→numeric→character >c(T,F,0) [1]100 >T+3 in arithmetic expression T F [1]4 are interpreted as 1 0,respectively >c(T,3,"abc") [1]"TRUE""3" "abc" When in doubt,experiment!! 15
Mode Coercion Mixed mode elements in expressions are coerced to a lowest common denominator mode mode order: logical −→ numeric −→ character > c(T,F,0) [1] 1 0 0 > T+3 # in arithmetic expression T & F [1] 4 # are interpreted as 1 & 0, respectively > c(T,3,"abc") [1] "TRUE" "3" "abc" When in doubt, experiment!! 15
Subvectors of Vectors >y [1]54321 y[c(5,3,1)]subvectors can be extracted by giving [1]135 the index positions as a vector >y[3:5] [1]321 >y[6] an nonexisting index position returns NA [1]NA >y[-(1:3)] negative index positions are omitted [1]21 while the rest are returned >y>3 [1] TRUE TRUE FALSE FALSEFALSE >y[y>3] we can also extract desired index positions [1]5 4 by specifying a logic vector of same length as y y[c(T,T,F,F,F)]this is an equivalent extraction [1]5 4 we get those elements with index TRUE or T 16
Subvectors of Vectors > y [1] 5 4 3 2 1 > y[c(5,3,1)] # subvectors can be extracted by giving [1] 1 3 5 # the index positions as a vector > y[3:5] [1] 3 2 1 > y[6] # an nonexisting index position returns NA [1] NA > y[-(1:3)] # negative index positions are omitted [1] 2 1 # while the rest are returned > y>3 [1] TRUE TRUE FALSE FALSE FALSE > y[y>3] # we can also extract desired index positions [1] 5 4 # by specifying a logic vector of same length as y > y[c(T,T,F,F,F)] # this is an equivalent extraction [1] 5 4 # we get those elements with index TRUE or T 16
Matrices mat=cbind(x,y)#cbind combines vectors of same length mat vertically positioned next to each other x y [1,]15 [2,]24 [3,]33 [4,]42 [5,]51 mat=cbind(x,y,y2) mat xy [1,]1525 [2,]2416 [3,]339 [4,]424 [5,5 11 all columns of a matrix have to have same length 17
Matrices > mat=cbind(x,y) # cbind combines vectors of same length > mat # vertically positioned next to each other x y [1,] 1 5 [2,] 2 4 [3,] 3 3 [4,] 4 2 [5,] 5 1 > mat=cbind(x,y,yˆ2) > mat x y [1,] 1 5 25 [2,] 2 4 16 [3,] 3 3 9 [4,] 4 2 4 [5,] 5 1 1 # all columns of a matrix have to have same length 17