Timothy Tickle
October 17th, 2015
sqrt(4) * sqrt(4) == 4
[1] TRUE
sqrt(2) * sqrt(2) == 2
[1] FALSE
all.equal( sqrt(2) * sqrt(2), 2)
[1] TRUE
The rules:
x <- 5
x
[1] 5
x * 2
[1] 10
x <- 2
x <- x + 1
x
[1] 3
x <- 2
x <- x + 1
y <- 4
x * y
[1] 12
Vector - Single collection of the same data mode
c(1,2,3,4,5,6,7)
[1] 1 2 3 4 5 6 7
5:9
[1] 5 6 7 8 9
9:1
[1] 9 8 7 6 5 4 3 2 1
c( "a", "a", "a", "a", "a" )
[1] "a" "a" "a" "a" "a"
rep( "a", 5 )
[1] "a" "a" "a" "a" "a"
c( "Cats","are","amazing" )
[1] "Cats" "are" "amazing"
c( TRUE, FALSE, TRUE, TRUE, FALSE )
[1] TRUE FALSE TRUE TRUE FALSE
factor( c( "Cats","are","still", "amazing" ) )
[1] Cats are still amazing
Levels: amazing are Cats still
c( 1, "2", FALSE)
[1] "1" "2" "FALSE"
c( 1, FALSE )
[1] 1 0
x <- 1:4
y <- 5:10
c( x, y )
[1] 1 2 3 4 5 6 7 8 9 10
c( 1:4, 5:10 )
[1] 1 2 3 4 5 6 7 8 9 10
x <- 1:4
x[ 2 ]
[1] 2
x <- 1:10
x[ 4:7 ]
[1] 4 5 6 7
x <- c( "a", "b", "c", "d", "e", "f" )
x[ c(5,3,1) ]
[1] "e" "c" "a"
x <- 5:1
x[ -1 ]
[1] 4 3 2 1
# Start with vector from 1 - 10
x <- 1:10
# Get indices of even elements
y <- x%%2 == 0
# Pull out those even element by index
x[y]
[1] 2 4 6 8 10
matrix( 1:20, nrow = 5, ncol = 4 )
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
boring.matrix <- matrix( 1:20, nrow = 5, ncol = 4 )
dim( boring.matrix )
[1] 5 4
boring.matrix[ ,1 ]
[1] 1 2 3 4 5
boring.matrix[ 2, ]
[1] 2 7 12 17
boring.matrix
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
boring.matrix[ 2, 1 ]
[1] 2
boring.matrix[ 2, 1 ] <- 99
boring.matrix
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 99 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
boring.matrix <- matrix(1:9, nrow = 3)
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
t(boring.matrix)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
boring.matrix + 1
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9
[3,] 4 7 10
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
boring.matrix + boring.matrix
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
boring.matrix * 2
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
boring.matrix * boring.matrix
[,1] [,2] [,3]
[1,] 1 16 49
[2,] 4 25 64
[3,] 9 36 81
boring.matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
boring.matrix %*% boring.matrix
[,1] [,2] [,3]
[1,] 30 66 102
[2,] 36 81 126
[3,] 42 96 150
colnames( boring.matrix ) <- c( "c1","c2","c3" )
rownames( boring.matrix ) <- c( "r1", "r2", "r3" )
boring.matrix
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
boring.matrix["r1",]
c1 c2 c3
1 4 7
x <- 11:16
y <- seq(0,1,.2)
z <- c( "one", "two", "three", "four", "five", "six" )
a <- factor( z )
data.frame(x,y,z,a)
x y z a
1 11 0.0 one one
2 12 0.2 two two
3 13 0.4 three three
4 14 0.6 four four
5 15 0.8 five five
6 16 1.0 six six
test.dataframe <- data.frame(x,y,z,a)
test.dataframe
x y z a
1 11 0.0 one one
2 12 0.2 two two
3 13 0.4 three three
4 14 0.6 four four
5 15 0.8 five five
6 16 1.0 six six
class( test.dataframe[3] )
[1] "data.frame"
class( test.dataframe[[1]] )
[1] "integer"
class( test.dataframe[[2]] )
[1] "numeric"
class( test.dataframe[[3]] )
[1] "factor"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
[18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
class( LETTERS )
[1] "character"
data.mode.df <- data.frame( LETTERS )
class( data.mode.df[[ 1 ]] )
[1] "factor"
mini.frame.one <- data.frame( "one" = 1:4 )
mini.frame.two <- data.frame( "two" = 6:9 )
cbind( mini.frame.one, mini.frame.two )
one two
1 1 6
2 2 7
3 3 8
4 4 9
test.dataframe
x y z a
1 11 0.0 one one
2 12 0.2 two two
3 13 0.4 three three
4 14 0.6 four four
5 15 0.8 five five
6 16 1.0 six six
test.dataframe[[1]] = 21:26
test.dataframe
x y z a
1 21 0.0 one one
2 22 0.2 two two
3 23 0.4 three three
4 24 0.6 four four
5 25 0.8 five five
6 26 1.0 six six
test.dataframe[[3,1]] = 111
test.dataframe
x y z a
1 21 0.0 one one
2 22 0.2 two two
3 111 0.4 three three
4 24 0.6 four four
5 25 0.8 five five
6 26 1.0 six six
So I have a person with:
[1] 1.3 1.6 3.2 9.8 10.2
[1] 13 6 4 7 6 5 8 9 7 4
[1] FALSE
[1] "Parent1.name" "Parent2.name"
measurements <- c( 1.3, 1.6, 3.2, 9.8, 10.2 )
self.reporting <- c( 13, 6, 4, 7, 6, 5, 8, 9, 7, 4 )
children <- FALSE
parents <- c( "Parent1.name", "Parent2.name" )
my.person <- list( measurements, self.reporting, children, parents )
my.person
[[1]]
[1] 1.3 1.6 3.2 9.8 10.2
[[2]]
[1] 13 6 4 7 6 5 8 9 7 4
[[3]]
[1] FALSE
[[4]]
[1] "Parent1.name" "Parent2.name"
my.person[4]
[[1]]
[1] "Parent1.name" "Parent2.name"
my.person[1:2]
[[1]]
[1] 1.3 1.6 3.2 9.8 10.2
[[2]]
[1] 13 6 4 7 6 5 8 9 7 4
my.person[[1]]
[1] 1.3 1.6 3.2 9.8 10.2
my.person <- list( measure = measurements, self.measure = self.reporting, child = children, parents = parents )
my.person
$measure
[1] 1.3 1.6 3.2 9.8 10.2
$self.measure
[1] 13 6 4 7 6 5 8 9 7 4
$child
[1] FALSE
$parents
[1] "Parent1.name" "Parent2.name"
my.person$parents
[1] "Parent1.name" "Parent2.name"
What data type would you use?
Oops, I may have missed somethings…
x = 1
if( x < 5){
print( "Mew." )
}
[1] "Mew."
x = 10
if( x < 3 ){
print( "Less than three!")
} else {
print( "Greater than or equal to three!")
}
[1] "Greater than or equal to three!"
x = 3
if( x < 3 ){
print( "Less than three!" )
} else if( x > 3 ) {
print( "Greater than three!")
} else {
print( "Equal to three." )
}
[1] "Equal to three."
measurements <- 1:10
for( value in measurements ){
print( value * 10 )
}
[1] 10
[1] 20
[1] 30
[1] 40
[1] 50
[1] 60
[1] 70
[1] 80
[1] 90
[1] 100
print( measurements * 10 )
[1] 10 20 30 40 50 60 70 80 90 100
Switches run code based on a key word
measures = rlnorm(1000)
centrality = "Mean"
#centrality = "Median"
#centrality = "Mew"
switch( centrality,
Mean = mean( measures ),
Median = median( measures ),
stop("Dave, I don't understand.")
)
[1] 1.661554
measures = 1:10
ifelse( measures < 5, 0, 1)
[1] 0 0 0 0 1 1 1 1 1 1
You should avoid loops as much as possible.
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
colSums( boring.matrix )
min( boring.matrix )
max( boring.matrix )
You should avoid loops as much as possible.
x = 11:20
x < 14
[1] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
x[ x < 14 ]
[1] 11 12 13
You should avoid loops as much as possible.
x
[1] 11 12 13 14 15 16 17 18 19 20
which( x < 14 )
[1] 1 2 3
x = 1:10
x[ c(1,3,5,7) ] <- NA
x
[1] NA 2 NA 4 NA 6 NA 8 9 10
x[ x == NA ] <- mean( x, na.rm = TRUE )
You should avoid loops as much as possible.
colSums( boring.matrix )
c1 c2 c3
6 15 24
apply( boring.matrix, 2, sum )
c1 c2 c3
6 15 24
You should avoid loops as much as possible.
rowSums( boring.matrix )
r1 r2 r3
12 15 18
apply( boring.matrix, 1, sum )
r1 r2 r3
12 15 18
arithmetic.means <- function( values.to.measure ){
measure.mean = sum( values.to.measure ) / length( values.to.measure )
return( measure.mean )
}
measurements <- 1:10
arithmetic.means( measurements )
[1] 5.5
mean( measurements )
[1] 5.5
critical.cat <- function( number ){
# Making functions like a boss
if( number %% 2 == 0 ){
print( "Mew" )
} else {
print( "Eww")
}
return( number - 1 )
}
new.df = read.table( "data/super_fun.txt" )
dim( new.df )
[1] 3 6
head( new.df )
new.df
new.df = read.table( "data/not_so_fun.txt" )
dim( new.df )
[1] 4 7
head( new.df )
V1 V2 V3 V4 V5 V6 V7
1 ID col_1 col_2 col_3 col_4 col_5 col_6
2 row_1 11 12 13 14 15 16
3 row_2 21 22 23 24 25 26
4 row_3 31 32 33 34 35 36
write.table( boring.matrix, "data/boring_matrix.txt")
write.csv( test.dataframe, "data/test_dataframe.csv", quote=FALSE)
library(xlsx)
read.xlsx( "data/super_fun.xlsx", 1 )
NA. col_1 col_2 col_3 col_4 col_5 col_6
1 row_1 11 12 13 14 15 16
2 row_2 21 22 23 24 25 26
3 row_3 31 32 33 34 35 36