3. Wk2_DataTypesVectorsAndSubsets2013

We subset rows and columns of data frames we subset

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n a different order •  Elements in a vector – this 6me with names concatenate > c(TRUE, FALSE) [1] TRUE FALSE > c(1.3, 2, 8/3) [1] 1.300000 2.000000 2.666667 > c("a", "z", "Hello") [1] "a" "z" "Hello" > y = c(100, 120) > c(x, y) bob alice john 3 2 1 100 120 •  We can use c() to make logical and character vectors •  No6ce that the last element determines the number of digits to display •  Character vecotr with 3 elements •  c() can be used to catenate vectors 1:3 returns a numeric vector of 1- apart values > 1:3 [1] 1 2 3 > 4:7 [1] 4 5 6 7 > 10:6 [1] 10 9 8 7 6 > 1.1:5.7 [1] 1.1 2.1 3.1 4.1 5.1 > 5.7:1.1 [1] 5.7 4.7 3.7 2.7 1.7 > 5.7:- 1.1 [1] 5.7 4.7 3.7 2.7 1.7 0.7 - 0.3 rep() > rep(3,2) [1] 3 3 > x = c(7,1,3) > rep(x, 2) [1] 7 1 3 7 1 3 > rep(x, 6mes = 2) [1] 7 1 3 7 1 3 > rep(x, c(3, 2, 1)) [1] 7 7 7 1 1 3 > rep(x, each = 2) [1] 7 7 1 1 3 3 •  Vector of two threes •  Arguments of rep can be vectors •  Repeat the vector 2 6mes •  Can use the argument name •  When 6mes argument is a vector then each element is repeated individually •  The Each argument seq() – a richer version of : > seq(1, 5, by = 2) [1] 1 3 5 > seq(1, 5, length = 3) [1] 1 3 5 > seq(1, 5, length = 5) [1] 1 2 3 4 5 > seq(1, length = 5, by = 2) [1] 1 3 5 7 9 > seq(1, 5, length = 5, by = 2) Error in seq.default(1, 5, length = 5, by = 2) : too many arguments •  seq() has several arguments •  from •  to •  by •  length •  There are many ways to call this func6on Ques6on: How could I produce the following vectors (without typing them all out)? 0 0 0 0 0 2 2 2 2 2 4 4 4 4 4 6 6 6 6 6 8 8 8 8 8 rep(seq(0, 8, by = 2), each = 5) 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 rep(1:5, 6mes = 5) 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 rep(1:5, 6mes = 5) + rep(0:4, each = 5) sort() and order() > fage [1] 77 33 79 47 27 33 67 52 59 2...
View Full Document

Ask a homework question - tutors are online