2013-06-29 11:37:14 +04:00
---
language: R
2013-07-04 09:59:13 +04:00
contributors:
- ["e99n09", "http://github.com/e99n09"]
2013-08-10 02:13:08 +04:00
- ["isomorphismes", "http://twitter.com/isomorphisms"]
2013-06-30 07:19:14 +04:00
filename: learnr.r
2013-06-29 11:37:14 +04:00
---
2014-05-23 23:37:15 +04:00
R is a statistical computing language. It has lots of libraries for uploading and cleaning data sets, running statistical procedures, and making graphs. You can also run `R` commands within a LaTeX document.
2013-06-29 11:37:14 +04:00
2014-09-08 15:08:28 +04:00
```r
2013-06-29 11:37:14 +04:00
2014-04-14 22:04:44 +04:00
# Comments start with number symbols.
2013-06-29 11:37:14 +04:00
2014-05-23 23:37:15 +04:00
# You can't make multi-line comments,
2013-06-29 11:37:14 +04:00
# but you can stack multiple comments like so.
2015-10-16 04:30:08 +03:00
# in Windows you can use CTRL-ENTER to execute a line.
# on Mac it is COMMAND-ENTER
2013-06-29 11:37:14 +04:00
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
#############################################################################
2013-08-09 01:50:52 +04:00
# Stuff you can do without understanding anything about programming
2014-05-23 23:37:15 +04:00
#############################################################################
# In this section, we show off some of the cool stuff you can do in
# R without understanding anything about programming. Do not worry
# about understanding everything the code does. Just enjoy!
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
data() # browse pre-loaded data sets
data(rivers) # get this one: "Lengths of Major North American Rivers"
ls() # notice that "rivers" now appears in the workspace
head(rivers) # peek at the data set
2013-08-09 01:50:52 +04:00
# 735 320 325 392 524 450
2014-05-23 23:37:15 +04:00
2013-08-09 01:50:52 +04:00
length(rivers) # how many rivers were measured?
# 141
2014-05-23 23:37:15 +04:00
summary(rivers) # what are some summary statistics?
2015-10-08 06:11:24 +03:00
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 135.0 310.0 425.0 591.2 680.0 3710.0
2014-05-23 23:37:15 +04:00
# make a stem-and-leaf plot (a histogram-like data visualization)
stem(rivers)
2013-08-09 01:50:52 +04:00
# The decimal point is 2 digit(s) to the right of the |
#
# 0 | 4
# 2 | 011223334555566667778888899900001111223333344455555666688888999
# 4 | 111222333445566779001233344567
# 6 | 000112233578012234468
# 8 | 045790018
# 10 | 04507
# 12 | 1471
# 14 | 56
# 16 | 7
# 18 | 9
2015-10-08 06:11:24 +03:00
# 20 |
2013-08-09 01:50:52 +04:00
# 22 | 25
# 24 | 3
2015-10-08 06:11:24 +03:00
# 26 |
# 28 |
# 30 |
# 32 |
# 34 |
2013-08-09 01:50:52 +04:00
# 36 | 1
2014-05-23 23:37:15 +04:00
stem(log(rivers)) # Notice that the data are neither normal nor log-normal!
# Take that, Bell curve fundamentalists.
2013-08-09 01:50:52 +04:00
# The decimal point is 1 digit(s) to the left of the |
#
# 48 | 1
2015-10-08 06:11:24 +03:00
# 50 |
2013-08-09 01:50:52 +04:00
# 52 | 15578
# 54 | 44571222466689
# 56 | 023334677000124455789
# 58 | 00122366666999933445777
# 60 | 122445567800133459
# 62 | 112666799035
# 64 | 00011334581257889
# 66 | 003683579
# 68 | 0019156
# 70 | 079357
# 72 | 89
# 74 | 84
# 76 | 56
# 78 | 4
2015-10-08 06:11:24 +03:00
# 80 |
2013-08-09 01:50:52 +04:00
# 82 | 2
2014-05-23 23:37:15 +04:00
# make a histogram:
hist(rivers, col="#333333", border="white", breaks=25) # play around with these parameters
hist(log(rivers), col="#333333", border="white", breaks=25) # you'll do more plotting later
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# Here's another neat data set that comes pre-loaded. R has tons of these.
2013-08-09 01:50:52 +04:00
data(discoveries)
2014-05-23 23:37:15 +04:00
plot(discoveries, col="#333333", lwd=3, xlab="Year",
main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
main="Number of important discoveries per year")
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# Rather than leaving the default ordering (by year),
# we could also sort to see what's typical:
2013-08-09 01:50:52 +04:00
sort(discoveries)
# [1] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
# [26] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3
# [51] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
# [76] 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 8 9 10 12
stem(discoveries, scale=2)
2015-10-08 06:11:24 +03:00
#
2013-08-09 01:50:52 +04:00
# The decimal point is at the |
#
# 0 | 000000000
# 1 | 000000000000
# 2 | 00000000000000000000000000
# 3 | 00000000000000000000
# 4 | 000000000000
# 5 | 0000000
# 6 | 000000
# 7 | 0000
# 8 | 0
# 9 | 0
# 10 | 0
2015-10-08 06:11:24 +03:00
# 11 |
2013-08-09 01:50:52 +04:00
# 12 | 0
max(discoveries)
# 12
summary(discoveries)
2015-10-08 06:11:24 +03:00
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.0 2.0 3.0 3.1 4.0 12.0
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# Roll a die a few times
2013-08-09 01:50:52 +04:00
round(runif(7, min=.5, max=6.5))
# 1 4 6 1 4 6 4
2014-05-23 23:37:15 +04:00
# Your numbers will differ from mine unless we set the same random.seed(31337)
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# Draw from a standard Gaussian 9 times
2013-08-09 01:50:52 +04:00
rnorm(9)
# [1] 0.07528471 1.03499859 1.34809556 -0.82356087 0.61638975 -1.88757271
# [7] -0.59975593 0.57629164 1.08455362
2014-05-23 23:37:15 +04:00
##################################################
# Data types and basic arithmetic
##################################################
# Now for the programming-oriented part of the tutorial.
# In this section you will meet the important data types of R:
# integers, numerics, characters, logicals, and factors.
# There are others, but these are the bare minimum you need to
# get started.
# INTEGERS
# Long-storage integers are written with L
5L # 5
class(5L) # "integer"
# (Try ?class for more information on the class() function.)
# In R, every single value, like 5L, is considered a vector of length 1
length(5L) # 1
# You can have an integer vector with length > 1 too:
c(4L, 5L, 8L, 3L) # 4 5 8 3
length(c(4L, 5L, 8L, 3L)) # 4
class(c(4L, 5L, 8L, 3L)) # "integer"
# NUMERICS
# A "numeric" is a double-precision floating-point number
5 # 5
class(5) # "numeric"
# Again, everything in R is a vector;
# you can make a numeric vector with more than one element
c(3,3,3,2,2,1) # 3 3 3 2 2 1
# You can use scientific notation too
5e4 # 50000
6.02e23 # Avogadro's number
1.6e-35 # Planck length
# You can also have infinitely large or small numbers
2013-08-09 01:50:52 +04:00
class(Inf) # "numeric"
2014-05-23 23:37:15 +04:00
class(-Inf) # "numeric"
2014-11-01 06:37:26 +03:00
# You might use "Inf", for example, in integrate(dnorm, 3, Inf);
2014-05-23 23:37:15 +04:00
# this obviates Z-score tables.
# BASIC ARITHMETIC
# You can do arithmetic with numbers
# Doing arithmetic on a mix of integers and numerics gives you another numeric
10L + 66L # 76 # integer plus integer gives integer
53.2 - 4 # 49.2 # numeric minus numeric gives numeric
2.0 * 2L # 4 # numeric times integer gives numeric
2014-05-24 16:54:39 +04:00
3L / 4 # 0.75 # integer over numeric gives numeric
2014-05-23 23:37:15 +04:00
3 %% 2 # 1 # the remainder of two numerics is another numeric
# Illegal arithmetic yeilds you a "not-a-number":
0 / 0 # NaN
class(NaN) # "numeric"
# You can do arithmetic on two vectors with length greater than 1,
# so long as the larger vector's length is an integer multiple of the smaller
c(1,2,3) + c(1,2,3) # 2 4 6
2013-06-29 11:37:14 +04:00
# CHARACTERS
# There's no difference between strings and characters in R
2014-05-23 23:37:15 +04:00
"Horatio" # "Horatio"
2013-08-09 01:50:52 +04:00
class("Horatio") # "character"
2014-05-23 23:37:15 +04:00
class('H') # "character"
# Those were both character vectors of length 1
# Here is a longer one:
c('alef', 'bet', 'gimmel', 'dalet', 'he')
# =>
# "alef" "bet" "gimmel" "dalet" "he"
length(c("Call","me","Ishmael")) # 3
# You can do regex operations on character vectors:
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
# R has several built-in character vectors:
letters
# =>
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
2013-08-09 01:50:52 +04:00
2013-06-29 11:37:14 +04:00
# LOGICALS
2014-05-23 23:37:15 +04:00
# In R, a "logical" is a boolean
2013-08-09 01:50:52 +04:00
class(TRUE) # "logical"
class(FALSE) # "logical"
2014-05-23 23:37:15 +04:00
# Their behavior is normal
2013-08-09 01:50:52 +04:00
TRUE == TRUE # TRUE
TRUE == FALSE # FALSE
FALSE != FALSE # FALSE
FALSE != TRUE # TRUE
2013-06-29 11:37:14 +04:00
# Missing data (NA) is logical, too
2013-08-09 01:50:52 +04:00
class(NA) # "logical"
2015-01-25 05:06:13 +03:00
# Use | and & for logic operations.
2015-01-20 21:34:18 +03:00
# OR
TRUE | FALSE # TRUE
# AND
TRUE & FALSE # FALSE
# You can test if x is TRUE
isTRUE(TRUE) # TRUE
2014-05-23 23:37:15 +04:00
# Here we get a logical vector with many elements:
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE
2013-06-29 11:37:14 +04:00
# FACTORS
# The factor class is for categorical data
2014-05-23 23:37:15 +04:00
# Factors can be ordered (like childrens' grade levels) or unordered (like gender)
2014-11-01 06:37:26 +03:00
factor(c("female", "female", "male", NA, "female"))
# female female male <NA> female
# Levels: female male
2014-05-23 23:37:15 +04:00
# The "levels" are the values the categorical data can take
2014-11-01 06:37:26 +03:00
# Note that missing data does not enter the levels
levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male"
2014-05-24 16:54:39 +04:00
# If a factor vector has length 1, its levels will have length 1, too
2014-05-23 23:37:15 +04:00
length(factor("male")) # 1
length(levels(factor("male"))) # 1
# Factors are commonly seen in data frames, a data structure we will cover later
data(infert) # "Infertility after Spontaneous and Induced Abortion"
levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
2014-05-24 16:54:39 +04:00
# NULL
# "NULL" is a weird one; use it to "blank out" a vector
2014-05-23 23:37:15 +04:00
class(NULL) # NULL
2015-01-29 08:27:09 +03:00
parakeet = c("beak", "feathers", "wings", "eyes")
2014-05-24 16:54:39 +04:00
parakeet
# =>
2015-10-08 06:11:24 +03:00
# [1] "beak" "feathers" "wings" "eyes"
2014-05-24 16:54:39 +04:00
parakeet < - NULL
parakeet
# =>
# NULL
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# TYPE COERCION
# Type-coercion is when you force a value to take on a different type
as.character(c(6, 8)) # "6" "8"
as.logical(c(1,0,1,1)) # TRUE FALSE TRUE TRUE
2014-05-24 16:54:39 +04:00
# If you put elements of different types into a vector, weird coercions happen:
2014-05-23 23:37:15 +04:00
c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog" "TRUE" "4"
as.numeric("Bilbo")
# =>
# [1] NA
# Warning message:
2015-10-08 06:11:24 +03:00
# NAs introduced by coercion
2013-08-09 01:50:52 +04:00
2014-05-23 23:37:15 +04:00
# Also note: those were just the basic data types
# There are many more data types, such as for dates, time series, etc.
2013-08-09 01:50:52 +04:00
2013-06-29 11:37:14 +04:00
2014-05-23 23:37:15 +04:00
##################################################
# Variables, loops, if/else
##################################################
# A variable is like a box you store a value in for later use.
# We call this "assigning" the value to the variable.
# Having variables lets us write loops, functions, and if/else statements
# VARIABLES
# Lots of way to assign stuff:
2013-06-29 11:37:14 +04:00
x = 5 # this is possible
y < - " 1 " # this is preferred
TRUE -> z # this works but is weird
# LOOPS
# We've got for loops
for (i in 1:4) {
print(i)
}
# We've got while loops
a < - 10
while (a > 4) {
cat(a, "...", sep = "")
a < - a - 1
}
# Keep in mind that for and while loops run slowly in R
# Operations on entire vectors (i.e. a whole row, a whole column)
# or apply()-type functions (we'll discuss later) are preferred
2013-07-02 06:00:22 +04:00
# IF/ELSE
# Again, pretty standard
if (4 > 3) {
2014-05-23 23:37:15 +04:00
print("4 is greater than 3")
2013-07-02 06:00:22 +04:00
} else {
2014-05-23 23:37:15 +04:00
print("4 is not greater than 3")
2013-07-02 06:00:22 +04:00
}
# =>
2014-05-23 23:37:15 +04:00
# [1] "4 is greater than 3"
2013-07-02 06:00:22 +04:00
2013-06-29 11:37:14 +04:00
# FUNCTIONS
# Defined like so:
2013-08-10 02:13:08 +04:00
jiggle < - function ( x ) {
2013-09-04 22:56:17 +04:00
x = x + rnorm(1, sd=.1) #add in a bit of (controlled) noise
2013-06-29 11:37:14 +04:00
return(x)
}
# Called like any other R function:
2013-08-10 02:13:08 +04:00
jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043
2013-06-29 11:37:14 +04:00
2014-05-23 23:37:15 +04:00
###########################################################################
# Data structures: Vectors, matrices, data frames, and arrays
###########################################################################
2013-06-29 11:37:14 +04:00
# ONE-DIMENSIONAL
2014-05-23 23:37:15 +04:00
# Let's start from the very beginning, and with something you already know: vectors.
2013-07-02 06:00:22 +04:00
vec < - c ( 8 , 9 , 10 , 11 )
2013-08-09 01:50:52 +04:00
vec # 8 9 10 11
2014-05-23 23:37:15 +04:00
# We ask for specific elements by subsetting with square brackets
# (Note that R starts counting from 1)
vec[1] # 8
letters[18] # "r"
LETTERS[13] # "M"
month.name[9] # "September"
c(6, 8, 7, 5, 3, 0, 9)[3] # 7
2013-07-02 06:00:22 +04:00
# We can also search for the indices of specific components,
2013-08-09 01:50:52 +04:00
which(vec %% 2 == 0) # 1 3
2014-05-24 16:54:39 +04:00
# grab just the first or last few entries in the vector,
2013-08-09 01:50:52 +04:00
head(vec, 1) # 8
2014-05-29 18:51:24 +04:00
tail(vec, 2) # 10 11
2014-05-23 23:37:15 +04:00
# or figure out if a certain value is in the vector
any(vec == 10) # TRUE
2013-06-29 11:37:14 +04:00
# If an index "goes over" you'll get NA:
2013-08-09 01:50:52 +04:00
vec[6] # NA
2013-07-02 06:00:22 +04:00
# You can find the length of your vector with length()
2013-08-09 01:50:52 +04:00
length(vec) # 4
2013-06-29 11:37:14 +04:00
# You can perform operations on entire vectors or subsets of vectors
2013-08-09 01:50:52 +04:00
vec * 4 # 16 20 24 28
vec[2:3] * 5 # 25 30
2014-05-23 23:37:15 +04:00
any(vec[2:3] == 8) # FALSE
2014-05-24 16:54:39 +04:00
# and R has many built-in functions to summarize vectors
2013-08-09 01:50:52 +04:00
mean(vec) # 9.5
var(vec) # 1.666667
2014-05-23 23:37:15 +04:00
sd(vec) # 1.290994
2013-08-09 01:50:52 +04:00
max(vec) # 11
min(vec) # 8
sum(vec) # 38
2014-05-23 23:37:15 +04:00
# Some more nice built-ins:
5:15 # 5 6 7 8 9 10 11 12 13 14 15
seq(from=0, to=31337, by=1337)
2014-05-24 16:54:39 +04:00
# =>
2014-05-23 23:37:15 +04:00
# [1] 0 1337 2674 4011 5348 6685 8022 9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751
2013-06-29 11:37:14 +04:00
# TWO-DIMENSIONAL (ALL ONE CLASS)
# You can make a matrix out of entries all of the same type like so:
mat < - matrix ( nrow = 3, ncol = 2, c ( 1 , 2 , 3 , 4 , 5 , 6 ) )
mat
# =>
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
# Unlike a vector, the class of a matrix is "matrix", no matter what's in it
class(mat) # => "matrix"
# Ask for the first row
2013-08-09 01:50:52 +04:00
mat[1,] # 1 4
2013-06-29 11:37:14 +04:00
# Perform operation on the first column
2013-08-09 01:50:52 +04:00
3 * mat[,1] # 3 6 9
2013-06-29 11:37:14 +04:00
# Ask for a specific cell
2013-08-09 01:50:52 +04:00
mat[3,2] # 6
2014-05-23 23:37:15 +04:00
2013-06-29 11:37:14 +04:00
# Transpose the whole matrix
t(mat)
# =>
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
2014-05-23 23:37:15 +04:00
# Matrix multiplication
mat %*% t(mat)
# =>
# [,1] [,2] [,3]
# [1,] 17 22 27
# [2,] 22 29 36
# [3,] 27 36 45
2013-06-29 11:37:14 +04:00
# cbind() sticks vectors together column-wise to make a matrix
mat2 < - cbind ( 1:4 , c ( " dog " , " cat " , " bird " , " dog " ) )
mat2
# =>
2015-10-08 06:11:24 +03:00
# [,1] [,2]
# [1,] "1" "dog"
# [2,] "2" "cat"
# [3,] "3" "bird"
2013-06-29 11:37:14 +04:00
# [4,] "4" "dog"
2013-08-09 01:50:52 +04:00
class(mat2) # matrix
2013-06-29 11:37:14 +04:00
# Again, note what happened!
# Because matrices must contain entries all of the same class,
# everything got converted to the character class
c(class(mat2[,1]), class(mat2[,2]))
# rbind() sticks vectors together row-wise to make a matrix
mat3 < - rbind ( c ( 1 , 2 , 4 , 5 ) , c ( 6 , 7 , 0 , 4 ) )
mat3
# =>
# [,1] [,2] [,3] [,4]
# [1,] 1 2 4 5
# [2,] 6 7 0 4
2014-05-24 16:54:39 +04:00
# Ah, everything of the same class. No coercions. Much better.
2013-06-29 11:37:14 +04:00
# TWO-DIMENSIONAL (DIFFERENT CLASSES)
2014-05-24 16:54:39 +04:00
# For columns of different types, use a data frame
2014-05-23 23:37:15 +04:00
# This data structure is so useful for statistical programming,
# a version of it was added to Python in the package "pandas".
students < - data . frame ( c ( " Cedric " , " Fred " , " George " , " Cho " , " Draco " , " Ginny " ) ,
c(3,2,2,1,0,-1),
c("H", "G", "G", "R", "S", "G"))
names(students) < - c ( " name " , " year " , " house " ) # name the columns
class(students) # "data.frame"
students
2013-06-29 11:37:14 +04:00
# =>
2014-05-23 23:37:15 +04:00
# name year house
# 1 Cedric 3 H
# 2 Fred 2 G
# 3 George 2 G
# 4 Cho 1 R
# 5 Draco 0 S
# 6 Ginny -1 G
class(students$year) # "numeric"
class(students[,3]) # "factor"
# find the dimensions
nrow(students) # 6
ncol(students) # 3
dim(students) # 6 3
2013-06-29 11:37:14 +04:00
# The data.frame() function converts character vectors to factor vectors
2014-05-23 23:37:15 +04:00
# by default; turn this off by setting stringsAsFactors = FALSE when
# you create the data.frame
?data.frame
2013-06-29 11:37:14 +04:00
# There are many twisty ways to subset data frames, all subtly unalike
2014-05-23 23:37:15 +04:00
students$year # 3 2 2 1 0 -1
students[,2] # 3 2 2 1 0 -1
students[,"year"] # 3 2 2 1 0 -1
2014-05-24 16:54:39 +04:00
# An augmented version of the data.frame structure is the data.table
2014-05-23 23:37:15 +04:00
# If you're working with huge or panel data, or need to merge a few data
# sets, data.table can be a good choice. Here's a whirlwind tour:
2014-05-24 16:54:39 +04:00
install.packages("data.table") # download the package from CRAN
require(data.table) # load it
2014-05-23 23:37:15 +04:00
students < - as . data . table ( students )
students # note the slightly different print-out
# =>
# name year house
# 1: Cedric 3 H
# 2: Fred 2 G
# 3: George 2 G
# 4: Cho 1 R
# 5: Draco 0 S
# 6: Ginny -1 G
2014-05-24 16:54:39 +04:00
students[name=="Ginny"] # get rows with name == "Ginny"
2014-05-23 23:37:15 +04:00
# =>
# name year house
# 1: Ginny -1 G
2014-05-24 16:54:39 +04:00
students[year==2] # get rows with year == 2
2014-05-23 23:37:15 +04:00
# =>
# name year house
# 1: Fred 2 G
# 2: George 2 G
2014-05-24 16:54:39 +04:00
# data.table makes merging two data sets easy
# let's make another data.table to merge with students
2014-05-23 23:37:15 +04:00
founders < - data . table ( house = c("G","H","R","S"),
founder=c("Godric","Helga","Rowena","Salazar"))
founders
# =>
# house founder
# 1: G Godric
# 2: H Helga
# 3: R Rowena
# 4: S Salazar
setkey(students, house)
setkey(founders, house)
2014-05-24 16:54:39 +04:00
students < - founders [ students ] # merge the two data sets by matching " house "
2014-05-23 23:37:15 +04:00
setnames(students, c("house","houseFounderName","studentName","year"))
students[,order(c("name","year","house","houseFounderName")), with=F]
# =>
# studentName year house houseFounderName
# 1: Fred 2 G Godric
# 2: George 2 G Godric
# 3: Ginny -1 G Godric
# 4: Cedric 3 H Helga
# 5: Cho 1 R Rowena
# 6: Draco 0 S Salazar
2013-06-29 11:37:14 +04:00
2014-05-24 16:54:39 +04:00
# data.table makes summary tables easy
2014-05-24 21:11:07 +04:00
students[,sum(year),by=house]
2014-05-24 16:54:39 +04:00
# =>
# house V1
# 1: G 3
# 2: H 3
# 3: R 1
# 4: S 0
# To drop a column from a data.frame or data.table,
# assign it the NULL value
students$houseFounderName < - NULL
students
# =>
# studentName year house
# 1: Fred 2 G
# 2: George 2 G
# 3: Ginny -1 G
# 4: Cedric 3 H
# 5: Cho 1 R
# 6: Draco 0 S
# Drop a row by subsetting
# Using data.table:
students[studentName != "Draco"]
# =>
# house studentName year
# 1: G Fred 2
# 2: G George 2
# 3: G Ginny -1
# 4: H Cedric 3
# 5: R Cho 1
# Using data.frame:
students < - as . data . frame ( students )
students[students$house != "G",]
# =>
# house houseFounderName studentName year
# 4 H Helga Cedric 3
# 5 R Rowena Cho 1
# 6 S Salazar Draco 0
# MULTI-DIMENSIONAL (ALL ELEMENTS OF ONE TYPE)
2013-06-29 11:37:14 +04:00
# Arrays creates n-dimensional tables
2014-05-24 16:54:39 +04:00
# All elements must be of the same type
2013-06-29 11:37:14 +04:00
# You can make a two-dimensional table (sort of like a matrix)
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
# [,1] [,2] [,3] [,4]
# [1,] 1 4 8 3
# [2,] 2 5 9 6
# You can use array to make three-dimensional matrices too
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
2013-08-11 16:11:44 +04:00
# [,1] [,2]
# [1,] 2 8
# [2,] 300 9
# [3,] 4 0
2013-06-29 11:37:14 +04:00
#
# , , 2
#
# [,1] [,2]
2013-08-11 16:11:44 +04:00
# [1,] 5 66
# [2,] 60 7
# [3,] 0 847
2013-06-29 11:37:14 +04:00
# LISTS (MULTI-DIMENSIONAL, POSSIBLY RAGGED, OF DIFFERENT TYPES)
# Finally, R has lists (of vectors)
2013-07-02 05:34:40 +04:00
list1 < - list ( time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # random
2013-06-29 11:37:14 +04:00
list1
# You can get items in the list like so
2014-05-23 23:37:15 +04:00
list1$time # one way
list1[["time"]] # another way
list1[[1]] # yet another way
# =>
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# [34] 34 35 36 37 38 39 40
# You can subset list items like any other vector
2013-06-29 11:37:14 +04:00
list1$price[4]
2014-05-23 23:37:15 +04:00
# Lists are not the most efficient data structure to work with in R;
# unless you have a very good reason, you should stick to data.frames
# Lists are often returned by functions that perform linear regressions
##################################################
2013-06-29 11:37:14 +04:00
# The apply() family of functions
2014-05-23 23:37:15 +04:00
##################################################
2013-06-29 11:37:14 +04:00
# Remember mat?
mat
# =>
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
# Use apply(X, MARGIN, FUN) to apply function FUN to a matrix X
# over rows (MAR = 1) or columns (MAR = 2)
# That is, R does FUN to each row (or column) of X, much faster than a
# for or while loop would do
2014-05-23 23:37:15 +04:00
apply(mat, MAR = 2, jiggle)
2013-06-29 11:37:14 +04:00
# =>
# [,1] [,2]
# [1,] 3 15
# [2,] 7 19
# [3,] 11 23
# Other functions: ?lapply, ?sapply
2013-07-01 19:40:06 +04:00
# Don't feel too intimidated; everyone agrees they are rather confusing
2013-06-29 11:37:14 +04:00
# The plyr package aims to replace (and improve upon!) the *apply() family.
install.packages("plyr")
require(plyr)
?plyr
2014-05-23 23:37:15 +04:00
2013-06-29 19:52:41 +04:00
#########################
2013-06-29 11:37:14 +04:00
# Loading data
2013-06-29 19:52:41 +04:00
#########################
2013-06-29 11:37:14 +04:00
# "pets.csv" is a file on the internet
2014-05-23 23:37:15 +04:00
# (but it could just as easily be be a file on your own computer)
2013-06-29 11:37:14 +04:00
pets < - read . csv ( " http: / / learnxinyminutes . com / docs / pets . csv " )
pets
head(pets, 2) # first two rows
tail(pets, 1) # last row
# To save a data frame or matrix as a .csv file
2013-06-29 19:52:41 +04:00
write.csv(pets, "pets2.csv") # to make a new .csv file
2013-06-29 11:37:14 +04:00
# set working directory with setwd(), look it up with getwd()
# Try ?read.csv and ?write.csv for more information
2014-05-23 23:37:15 +04:00
2013-06-29 19:52:41 +04:00
#########################
2013-06-29 11:37:14 +04:00
# Plots
2013-06-29 19:52:41 +04:00
#########################
2013-06-29 11:37:14 +04:00
2014-05-23 23:37:15 +04:00
# BUILT-IN PLOTTING FUNCTIONS
2013-06-29 11:37:14 +04:00
# Scatterplots!
plot(list1$time, list1$price, main = "fake data")
2013-07-02 06:00:22 +04:00
# Regressions!
linearModel < - lm ( price ~ time , data = list1)
linearModel # outputs result of regression
2013-06-29 11:37:14 +04:00
# Plot regression line on existing plot
2013-07-02 06:00:22 +04:00
abline(linearModel, col = "red")
2013-06-29 11:37:14 +04:00
# Get a variety of nice diagnostics
2013-07-02 06:00:22 +04:00
plot(linearModel)
2013-06-29 11:37:14 +04:00
# Histograms!
hist(rpois(n = 10000, lambda = 5), col = "thistle")
# Barplots!
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))
2014-05-23 23:37:15 +04:00
# GGPLOT2
# But these are not even the prettiest of R's plots
2013-06-29 11:37:14 +04:00
# Try the ggplot2 package for more and better graphics
install.packages("ggplot2")
require(ggplot2)
?ggplot2
2014-05-23 23:37:15 +04:00
pp < - ggplot ( students , aes ( x = house))
pp + geom_histogram()
ll < - as . data . table ( list1 )
pp < - ggplot ( ll , aes ( x = time,price))
pp + geom_point()
# ggplot2 has excellent documentation (available http://docs.ggplot2.org/current/)
2013-06-29 11:37:14 +04:00
```
2013-07-02 06:00:22 +04:00
## How do I get R?
2013-06-29 11:37:14 +04:00
2013-07-02 06:00:22 +04:00
* Get R and the R GUI from [http://www.r-project.org/ ](http://www.r-project.org/ )
* [RStudio ](http://www.rstudio.com/ide/ ) is another GUI