This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Learning R Patrick Lam setting up I download R from CRAN I work in the console (code not saved) I open a script, type code in script, and save as a .R file Example code is in red R as calculator > 5 + 4 [1] 9 > 8 * 2  sqrt(9) [1] 13 > log(4)/9^2 [1] 0.01711 objects R is an objectoriented programming language. Use < as assignment operator for objects. > 5 + 4 [1] 9 > my.sum < 5 + 4 > my.sum [1] 9 > my.name < "Patrick" > my.name [1] "Patrick" vectors All objects consist of one or more vectors . vector: a combination of elements (i.e. numbers, words), usually created using c() , seq() , or rep() > empty.vector < c() > empty.vector NULL > one.to.five < c(1, 2, 3, 4, 5) > one.to.five [1] 1 2 3 4 5 > poli.sci < c("theory", "amer.", "comp.", "ir") > poli.sci [1] "theory" "amer." "comp." "ir" > one.to.ten < 1:10 > one.to.ten [1] 1 2 3 4 5 6 7 8 9 10 > two.to.five < seq(from = 2, to = 5, by = 1) > two.to.five [1] 2 3 4 5 > all.fours < rep(4, times = 5) > all.fours [1] 4 4 4 4 4 All elements in a vector must be of the same data type! data types I numeric I character I logical numeric: numbers > three < 3 > three [1] 3 > is.numeric(three) [1] TRUE > as.numeric("3") [1] 3 character: for example, words or phrases (must be in ””) > president < "Barack Obama" > president [1] "Barack Obama" > is.character(president) [1] TRUE > as.character(3) [1] "3" logical: TRUE (T) or FALSE (F) > num.vec < c(5, 6, 4) > logical.vec < num.vec == 6 > logical.vec [1] FALSE TRUE FALSE > is.logical(logical.vec) [1] TRUE can also be represented as numeric 1 or 0: > as.numeric(logical.vec) [1] 0 1 0 All elements in a vector must be of the same data type! I if a vector has a character element, all elements become character > mixed.vec < c(5, "Patrick", TRUE) > mixed.vec [1] "5" "Patrick" "TRUE" I if a vector has both numeric and logical elements, all elements become numeric > mixed.vec2 < c(10, FALSE) > mixed.vec2 [1] 10 character > numeric > logical object classes All objects consist of one or more vectors. In addition to vector, objects can be of one of the following classes: I matrix I array I dataframe I list matrix A matrix is a twodimensional ( r × c ) object (think a bunch of stacked or sidebyside vectors). > a.matrix < matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) > a.matrix [,1] [,2] [1,] 1 3 [2,] 2 4 > class(a.matrix) [1] "matrix" All elements in a matrix must be of the same data type. character > numeric > logical array An array is a threedimensional ( r × c × h ) object (think a bunch of stacked r × c matrices). All elements in an array must be of the same data type (character > numeric > logical). > an.array < array(0, dim = c(2, 2, 3)) > an.array , , 1 [,1] [,2] [1,] [2,] , , 2 [,1] [,2] [1,] [2,] , , 3 [,1] [,2] [1,] [2,] dataframe A dataframe is a twodimensional ( r × c × h ) object (like a matrix)....
View
Full
Document
This note was uploaded on 05/12/2010 for the course APPLIED ST 2010 taught by Professor Various during the Spring '10 term at Universidad Nacional Agraria La Molina.
 Spring '10
 Various

Click to edit the document details