Basics of R

John Bastiaansen

R Packages

install.packages("reshape")

Note the use of quotes around the name of the package

require(reshape)

R Packages

Packages tab

R Packages

Select CRAN mirror

R Packages

Select package

Objects

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"

Object class types

We introduce these classes:

Some or all of these will sound familiar because they are not specific to R

Vector

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"

Vector

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

Syntax notes

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"

Functions to make vectors

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

Factor

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

Matrix

A matrix:

matrix1 <- 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

Matrix elements

Elements 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.

list

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

list

record$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

vector3 <- c("a", "a", "c", "d")
matrix3 <- matrix(1:20, nrow = 4, ncol = 5)
data1 <- data.frame(vector3, matrix3)
class(data1)
## [1] "data.frame"

function

ls()
search()
matrix(1:20, nrow = 4, ncol = 5, byrow = FALSE)

Syntax notes

You 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)

End