John Bastiaansen
install.packages to install a package and subsequently function require to load a package:install.packages("reshape")Note the use of quotes around the name of the package
require(reshape)Packages in the lower right panelInstall Packagesclass(object) shows the class, or type, of an object
numeric, logical, charactermatrix, factor and data.framels() shows all the objects in the workspace.ls()## [1] "a" "action" "animals" "animals1"
## [5] "animals100p" "b" "bhat" "bp"
## [9] "bp1" "ChickWeight" "col" "colors"
## [13] "CW" "d" "dd" "df"
## [17] "dir" "elegantCS" "i" "ii"
## [21] "indices" "lineTypes" "mat" "mat1"
## [25] "mat2" "mat3" "mat4" "matchall"
## [29] "meansd" "mns" "n" "nDiets"
## [33] "nnstring" "nstring" "p" "p1"
## [37] "p2" "p3" "p4" "p5"
## [41] "p6" "plotChar" "q5" "q95"
## [45] "s" "seq1" "seq2" "sstring"
## [49] "string" "sw" "swl" "t"
## [53] "text" "tidy.opts" "today" "Today"
## [57] "todaysplitted" "tt" "ttFancy" "uglyCS"
## [61] "v1" "v2" "v22" "v3"
## [65] "v4" "v5" "vec1" "vec2"
## [69] "vv" "weights" "x" "y"
We introduce these classes:
vectorfactormatrixlistdata.framefunctionSome or all of these will sound familiar because they are not specific to R
A vector is an ordered collection of data of the same type. We make 2 vectors with the function c() :
vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
vector1## [1] 1 2 3 4 5 6 7 8 9 10
vector2 <- c("a", "b", "c", "d", "e", "b", "a", "c", "b", "d")
vector2## [1] "a" "b" "c" "d" "e" "b" "a" "c" "b" "d"
Several functions exist to obtain information about vectors that were made :
class(vector1)## [1] "numeric"
class(vector2)## [1] "character"
length(vector1)## [1] 10
length(vector2)## [1] 10
<- points to the object receiving the value of the expression= operator can be used (but not always)object <- c("a", "b", "c", "d")
object## [1] "a" "b" "c" "d"
## OR equivalently
object = c("a", "b", "c", "d")
object## [1] "a" "b" "c" "d"
We may encounter several functions to create vectors:
x <- 1:10
assign("x", 1:10)
x <- seq(1, 10, by = 1)
x <- seq(length = 10, from = 1, by = 1)
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)Choose an efficient and convenient method, the results will be the same
x## [1] 1 2 3 4 5 6 7 8 9 10
A factor is an efficient way to store character data where elements are repeated, for instance when they represent grouping of the data, like for instance the name of a treatment.
x1 <- c("a", "b", "c", "a", "b", "c", "a", "c")
x1## [1] "a" "b" "c" "a" "b" "c" "a" "c"
x2 <- as.factor(x1)
x2## [1] a b c a b c a c
## Levels: a b c
levels(x2) <- c("low", "mid", "high")
x2## [1] low mid high low mid high low high
## Levels: low mid high
A matrix:
numeric or charactermatrix1 <- matrix(1:20, nrow = 2, ncol = 10)
matrix1## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 3 5 7 9 11 13 15 17 19
## [2,] 2 4 6 8 10 12 14 16 18 20
matrix2 <- matrix(1:20, nrow = 2, ncol = 10, byrow = TRUE)
matrix2## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 11 12 13 14 15 16 17 18 19 20
byrow = TRUE for matrix2Elements in matrix can be accessed as matrix[x,y]:
matrix2## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 11 12 13 14 15 16 17 18 19 20
matrix2[2, 3]## [1] 13
matrix2[2, ]## [1] 11 12 13 14 15 16 17 18 19 20
matrix1[, 2]## [1] 3 4
Accessing elements by their index will be used in the section on programming.
record <- list(name = "Fred", spouse = "Mary", no.children = 3, child.ages = c(4,7, 9))
record## $name
## [1] "Fred"
##
## $spouse
## [1] "Mary"
##
## $no.children
## [1] 3
##
## $child.ages
## [1] 4 7 9
$ or the double square brackets operator [[]][] will give a subset of the listrecord$name## [1] "Fred"
record$child.ages## [1] 4 7 9
record[1:2]## $name
## [1] "Fred"
##
## $spouse
## [1] "Mary"
record[[1]]## [1] "Fred"
data.frame is a matrix where the columns can be of different classes numeric, character, factor, …[ and ] operators are used as in a matrixdata.frame can be accessed with the $vector3 <- c("a", "a", "c", "d")
matrix3 <- matrix(1:20, nrow = 4, ncol = 5)
data1 <- data.frame(vector3, matrix3)
class(data1)## [1] "data.frame"
functionName(argument1, argument2, argument3, ...)ls()
search()
matrix(1:20, nrow = 4, ncol = 5, byrow = FALSE)+ sign instead of the normal > prompt sign to indicate a continuing statementESC on Windows, or Ctrl+C on Linux to quit inputting the commandYou can write commands in one line or in more lines, as long as it is clear that they belong together:
matrix4 <- matrix(1:20, nrow = 4, ncol = 5)
matrix4 <- matrix(1:20, nrow = 4,
ncol = 5)