From 29bbaff5a6ed60f40cc710a6ad79434de4d95d75 Mon Sep 17 00:00:00 2001 From: i Date: Thu, 8 Aug 2013 14:52:59 -0400 Subject: wording Compiling in LaTeX isn't that easy, actually... --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 0240e8fb..3339a07e 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -5,7 +5,7 @@ contributors: filename: learnr.r --- -R is a statistical computing language. It has lots of good built-in functions for uploading and cleaning data sets, running common statistical tests, and making graphs. You can also easily compile it within a LaTeX document. +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. ```python -- cgit v1.2.3 From ee1b3546ad1a1a0601f2dc413d0b96f345c27ad9 Mon Sep 17 00:00:00 2001 From: i Date: Thu, 8 Aug 2013 17:50:52 -0400 Subject: Update r.html.markdown significant changes. style changes (no !, no =>). content additions. start by showing off R's non-programming features before getting to the language per se. --- r.html.markdown | 311 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 246 insertions(+), 65 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 0240e8fb..61140be5 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -16,61 +16,242 @@ R is a statistical computing language. It has lots of good built-in functions fo # Hit COMMAND-ENTER to execute a line + +################################################################### +# Stuff you can do without understanding anything about programming +################################################################### + +data() # Browse pre-loaded data sets +data(rivers) # Lengths of Major North American Rivers +ls() # Notice that "rivers" appears in the workspace +head(rivers) # peek at the dataset +# 735 320 325 392 524 450 +length(rivers) # how many rivers were measured? +# 141 +summary(rivers) +# Min. 1st Qu. Median Mean 3rd Qu. Max. +# 135.0 310.0 425.0 591.2 680.0 3710.0 +stem(rivers) #stem-and-leaf plot (like a histogram) +# +# 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 +# 20 | +# 22 | 25 +# 24 | 3 +# 26 | +# 28 | +# 30 | +# 32 | +# 34 | +# 36 | 1 + + +stem(log(rivers)) #Notice that the data are neither normal nor log-normal! Take that, Bell Curve fundamentalists. + +# The decimal point is 1 digit(s) to the left of the | +# +# 48 | 1 +# 50 | +# 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 +# 80 | +# 82 | 2 + + +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 + +#Here's another neat data set that comes pre-loaded. R has tons of these. data() +data(discoveries) +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") + + +#rather than leaving the default ordering (by year) we could also sort to see what's typical +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) +# +# 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 +# 11 | +# 12 | 0 + +max(discoveries) +# 12 + +summary(discoveries) +# Min. 1st Qu. Median Mean 3rd Qu. Max. +# 0.0 2.0 3.0 3.1 4.0 12.0 + + + + +#Basic statistical operations don't require any programming knowledge either + +#roll a die a few times +round(runif(7, min=.5, max=6.5)) +# 1 4 6 1 4 6 4 + +#your numbers will differ from mine unless we set the same random.seed(31337) + + +#draw from a standard Gaussian 9 times +rnorm(9) +# [1] 0.07528471 1.03499859 1.34809556 -0.82356087 0.61638975 -1.88757271 +# [7] -0.59975593 0.57629164 1.08455362 + + + + + + + + + ######################### -# The absolute basics +# Basic programming stuff ######################### # NUMBERS -# We've got doubles! Behold the "numeric" class -5 # => [1] 5 -class(5) # => [1] "numeric" -# We've also got integers! They look suspiciously similar, -# but indeed are different -5L # => [1] 5 -class(5L) # => [1] "integer" +# "numeric" means double-precision floating-point numbers +5 # 5 +class(5) # "numeric" +5e4 # 50000 #handy when dealing with large,small,or variable orders of magnitude +6.02e23 # Avogadro's number +1.6e-35 # Planck length + +# long-storage integers are written with L +5L # 5 +class(5L) # "integer" + # Try ?class for more information on the class() function -# In fact, you can look up the documentation on just about anything with ? +# In fact, you can look up the documentation on `xyz` with ?xyz +# or see the source for `xyz` by evaluating xyz + +# Arithmetic +10 + 66 # 76 +53.2 - 4 # 49.2 +2 * 2.0 # 4 +3L / 4 # 0.75 +3 %% 2 # 1 + +# Weird number types +class(NaN) # "numeric" +class(Inf) # "numeric" +class(-Inf) # "numeric" #used in for example integrate( dnorm(x), 3, Inf ) -- which obviates Z-score tables + +# but beware, NaN isn't the only weird type... +class(NA) # see below +class(NULL) # NULL + + +# SIMPLE LISTS +c(6, 8, 7, 5, 3, 0, 9) # 6 8 7 5 3 0 9 +c('alef', 'bet', 'gimmel', 'dalet', 'he') # "alef" "bet" "gimmel" "dalet" "he" +c('Z', 'o', 'r', 'o') == "Zoro" # FALSE FALSE FALSE FALSE + +#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) +# [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 + +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" + + +# Access the n'th element of a list with list.name[n] or sometimes list.name[[n]] +letters[18] # "r" +LETTERS[13] # "M" +month.name[9] # "September" +c(6, 8, 7, 5, 3, 0, 9)[3] # 7 -# All the normal operations! -10 + 66 # => [1] 76 -53.2 - 4 # => [1] 49.2 -2 * 2.0 # => [1] 4 -3L / 4 # => [1] 0.75 -3 %% 2 # => [1] 1 -# Finally, we've got not-a-numbers! They're numerics too -class(NaN) # => [1] "numeric" # CHARACTERS -# We've (sort of) got strings! Behold the "character" class -"plugh" # => [1] "plugh" -class("plugh") # "character" # There's no difference between strings and characters in R +"Horatio" # "Horatio" +class("Horatio") # "character" +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." + + + # LOGICALS -# We've got booleans! Behold the "logical" class -class(TRUE) # => [1] "logical" -class(FALSE) # => [1] "logical" +# booleans +class(TRUE) # "logical" +class(FALSE) # "logical" # Behavior is normal -TRUE == TRUE # => [1] TRUE -TRUE == FALSE # => [1] FALSE -FALSE != FALSE # => [1] FALSE -FALSE != TRUE # => [1] TRUE +TRUE == TRUE # TRUE +TRUE == FALSE # FALSE +FALSE != FALSE # FALSE +FALSE != TRUE # TRUE # Missing data (NA) is logical, too -class(NA) # => [1] "logical" +class(NA) # "logical" + + # FACTORS # The factor class is for categorical data -# It has an attribute called levels that describes all the possible categories -factor("dog") -# => -# [1] dog -# Levels: dog -# (This will make more sense once we start talking about vectors) +# which can be ordered (like childrens' grade levels) +# or unordered (like gender) +levels(factor(c("female", "male", "male", "female", "NA", "female"))) # "female" "male" "NA" + +factor(c("female", "female", "male", "NA", "female")) +# female female male NA female +# Levels: female male NA + +data(infert) #Infertility after Spontaneous and Induced Abortion +levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" + + # VARIABLES @@ -80,8 +261,8 @@ y <- "1" # this is preferred TRUE -> z # this works but is weird # We can use coerce variables to different classes -as.numeric(y) # => [1] 1 -as.character(x) # => [1] "5" +as.numeric(y) # 1 +as.character(x) # "5" # LOOPS @@ -122,7 +303,7 @@ myFunc <- function(x) { } # Called like any other R function: -myFunc(5) # => [1] 19 +myFunc(5) # 19 ######################### # Fun with data: vectors, matrices, data frames, and arrays @@ -132,35 +313,35 @@ myFunc(5) # => [1] 19 # You can vectorize anything, so long as all components have the same type vec <- c(8, 9, 10, 11) -vec # => [1] 8 9 10 11 +vec # 8 9 10 11 # The class of a vector is the class of its components -class(vec) # => [1] "numeric" +class(vec) # "numeric" # If you vectorize items of different classes, weird coercions happen -c(TRUE, 4) # => [1] 1 4 -c("dog", TRUE, 4) # => [1] "dog" "TRUE" "4" +c(TRUE, 4) # 1 4 +c("dog", TRUE, 4) # "dog" "TRUE" "4" # We ask for specific components like so (R starts counting from 1) -vec[1] # => [1] 8 +vec[1] # 8 # We can also search for the indices of specific components, -which(vec %% 2 == 0) # => [1] 1 3 +which(vec %% 2 == 0) # 1 3 # or grab just the first or last entry in the vector -head(vec, 1) # => [1] 8 -tail(vec, 1) # => [1] 11 +head(vec, 1) # 8 +tail(vec, 1) # 11 # If an index "goes over" you'll get NA: -vec[6] # => [1] NA +vec[6] # NA # You can find the length of your vector with length() -length(vec) # => [1] 4 +length(vec) # 4 # You can perform operations on entire vectors or subsets of vectors -vec * 4 # => [1] 16 20 24 28 -vec[2:3] * 5 # => [1] 25 30 +vec * 4 # 16 20 24 28 +vec[2:3] * 5 # 25 30 # and there are many built-in functions to summarize vectors -mean(vec) # => [1] 9.5 -var(vec) # => [1] 1.666667 -sd(vec) # => [1] 1.290994 -max(vec) # => [1] 11 -min(vec) # => [1] 8 -sum(vec) # => [1] 38 +mean(vec) # 9.5 +var(vec) # 1.666667 +sd(vec) # 1.290994 +max(vec) # 11 +min(vec) # 8 +sum(vec) # 38 # TWO-DIMENSIONAL (ALL ONE CLASS) @@ -175,11 +356,11 @@ mat # Unlike a vector, the class of a matrix is "matrix", no matter what's in it class(mat) # => "matrix" # Ask for the first row -mat[1,] # => [1] 1 4 +mat[1,] # 1 4 # Perform operation on the first column -3 * mat[,1] # => [1] 3 6 9 +3 * mat[,1] # 3 6 9 # Ask for a specific cell -mat[3,2] # => [1] 6 +mat[3,2] # 6 # Transpose the whole matrix t(mat) # => @@ -196,7 +377,7 @@ mat2 # [2,] "2" "cat" # [3,] "3" "bird" # [4,] "4" "dog" -class(mat2) # => [1] matrix +class(mat2) # matrix # Again, note what happened! # Because matrices must contain entries all of the same class, # everything got converted to the character class @@ -216,7 +397,7 @@ mat3 # For columns of different classes, use the data frame dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog")) names(dat) <- c("number", "species") # name the columns -class(dat) # => [1] "data.frame" +class(dat) # "data.frame" dat # => # number species @@ -224,14 +405,14 @@ dat # 2 2 cat # 3 1 bird # 4 4 dog -class(dat$number) # => [1] "numeric" -class(dat[,2]) # => [1] "factor" +class(dat$number) # "numeric" +class(dat[,2]) # "factor" # The data.frame() function converts character vectors to factor vectors # There are many twisty ways to subset data frames, all subtly unalike -dat$number # => [1] 5 2 1 4 -dat[,1] # => [1] 5 2 1 4 -dat[,"number"] # => [1] 5 2 1 4 +dat$number # 5 2 1 4 +dat[,1] # 5 2 1 4 +dat[,"number"] # 5 2 1 4 # MULTI-DIMENSIONAL (ALL OF ONE CLASS) -- cgit v1.2.3 From fbfca446ff92ab9dec38569cecf250b82ab4aabc Mon Sep 17 00:00:00 2001 From: i Date: Thu, 8 Aug 2013 17:51:32 -0400 Subject: Update r.html.markdown ...only in Windows --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 61140be5..5f143150 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -14,7 +14,7 @@ R is a statistical computing language. It has lots of good built-in functions fo # You can't make a multi-line comment per se, # but you can stack multiple comments like so. -# Hit COMMAND-ENTER to execute a line +# in Windows, hit COMMAND-ENTER to execute a line ################################################################### -- cgit v1.2.3 From f0d771ddea5537e98ef47462c5c8c41b0314f856 Mon Sep 17 00:00:00 2001 From: i Date: Fri, 9 Aug 2013 18:13:08 -0400 Subject: =?UTF-8?q?Bender=20Bending=20Rodr=C3=ADguez?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's make the function example do something a little more interesting. --- r.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index dd94072b..e7921299 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -2,6 +2,7 @@ language: R contributors: - ["e99n09", "http://github.com/e99n09"] + - ["isomorphismes", "http://twitter.com/isomorphisms"] filename: learnr.r --- @@ -296,14 +297,13 @@ if (4 > 3) { # FUNCTIONS # Defined like so: -myFunc <- function(x) { - x <- x * 4 - x <- x - 1 +jiggle <- function(x) { + x+ rnorm(x, sd=.1) #add in a bit of (controlled) noise return(x) } # Called like any other R function: -myFunc(5) # 19 +jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043 ######################### # Fun with data: vectors, matrices, data frames, and arrays -- cgit v1.2.3 From 2f1db5d8ae8a8c437e9c225211dadaf7b1f0c58d Mon Sep 17 00:00:00 2001 From: Johannes Choo Date: Sun, 11 Aug 2013 20:11:44 +0800 Subject: fixed wrong sample output in r.htl.markdown --- r.html.markdown | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index dd94072b..e4adb5dc 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -428,15 +428,17 @@ 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 # -# [,1] [,2] -# [1,] 1 4 -# [2,] 2 5 +# [,1] [,2] +# [1,] 2 8 +# [2,] 300 9 +# [3,] 4 0 # # , , 2 # # [,1] [,2] -# [1,] 8 1 -# [2,] 9 2 +# [1,] 5 66 +# [2,] 60 7 +# [3,] 0 847 # LISTS (MULTI-DIMENSIONAL, POSSIBLY RAGGED, OF DIFFERENT TYPES) -- cgit v1.2.3 From 358f357a46f2a6c72014a9d7e5e5d6d46c4924df Mon Sep 17 00:00:00 2001 From: Tom Hebbron Date: Wed, 4 Sep 2013 19:56:17 +0100 Subject: Update r.html.markdown Fixed a problem with the jiggle function - variable x wasn't being updated before being returned, and the rnorm function was being asked to return x output values, rather than one. After testing, the values given the documentation are now correct. --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 03aa1dd2..9c17e8ca 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -298,7 +298,7 @@ if (4 > 3) { # Defined like so: jiggle <- function(x) { - x+ rnorm(x, sd=.1) #add in a bit of (controlled) noise + x = x + rnorm(1, sd=.1) #add in a bit of (controlled) noise return(x) } -- cgit v1.2.3 From 777c333ff078a7095f9b7ac303829b249f499b21 Mon Sep 17 00:00:00 2001 From: Sam Dodrill Date: Mon, 14 Apr 2014 11:04:44 -0700 Subject: Remove references to hash and hashtag in favor of number symbol --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 9c17e8ca..ea94ae42 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -10,7 +10,7 @@ R is a statistical computing language. It has lots of libraries for uploading an ```python -# Comments start with hashtags. +# Comments start with number symbols. # You can't make a multi-line comment per se, # but you can stack multiple comments like so. -- cgit v1.2.3 From 31c74615e6a492db39e1586c3d4aaa7c4ada5e56 Mon Sep 17 00:00:00 2001 From: e99n09 Date: Fri, 23 May 2014 15:37:15 -0400 Subject: Significant new content I moved some things around. I preserved isomorphismes' "no programming knowledge first" approach; I really like this. In the "programming-heavy" part of the tutorial, I emphasized from the very beginning that every "single" thing in R, like 4 or "foo", is really just a vector of length 1. I added in some whirlwind tours of data.table and ggplot2. --- r.html.markdown | 468 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 293 insertions(+), 175 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index ea94ae42..dfc945c1 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -6,34 +6,42 @@ contributors: filename: learnr.r --- -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. +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. ```python # Comments start with number symbols. -# You can't make a multi-line comment per se, +# You can't make multi-line comments, # but you can stack multiple comments like so. -# in Windows, hit COMMAND-ENTER to execute a line +# in Windows or Mac, hit COMMAND-ENTER to execute a line -################################################################### + +############################################################################# # Stuff you can do without understanding anything about programming -################################################################### +############################################################################# + +# 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! -data() # Browse pre-loaded data sets -data(rivers) # Lengths of Major North American Rivers -ls() # Notice that "rivers" appears in the workspace -head(rivers) # peek at the dataset +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 # 735 320 325 392 524 450 + length(rivers) # how many rivers were measured? # 141 -summary(rivers) +summary(rivers) # what are some summary statistics? # Min. 1st Qu. Median Mean 3rd Qu. Max. # 135.0 310.0 425.0 591.2 680.0 3710.0 -stem(rivers) #stem-and-leaf plot (like a histogram) -# + +# make a stem-and-leaf plot (a histogram-like data visualization) +stem(rivers) + # The decimal point is 2 digit(s) to the right of the | # # 0 | 4 @@ -56,8 +64,8 @@ stem(rivers) #stem-and-leaf plot (like a histogram) # 34 | # 36 | 1 - -stem(log(rivers)) #Notice that the data are neither normal nor log-normal! Take that, Bell Curve fundamentalists. +stem(log(rivers)) # Notice that the data are neither normal nor log-normal! +# Take that, Bell curve fundamentalists. # The decimal point is 1 digit(s) to the left of the | # @@ -80,17 +88,19 @@ stem(log(rivers)) #Notice that the data are neither normal nor log-normal! Take # 80 | # 82 | 2 +# 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 -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 - -#Here's another neat data set that comes pre-loaded. R has tons of these. data() +# Here's another neat data set that comes pre-loaded. R has tons of these. data(discoveries) -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") +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") - -#rather than leaving the default ordering (by year) we could also sort to see what's typical +# Rather than leaving the default ordering (by year), +# we could also sort to see what's typical: 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 @@ -117,231 +127,249 @@ stem(discoveries, scale=2) max(discoveries) # 12 - summary(discoveries) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.0 2.0 3.0 3.1 4.0 12.0 - - - -#Basic statistical operations don't require any programming knowledge either - -#roll a die a few times +# Roll a die a few times round(runif(7, min=.5, max=6.5)) # 1 4 6 1 4 6 4 +# Your numbers will differ from mine unless we set the same random.seed(31337) -#your numbers will differ from mine unless we set the same random.seed(31337) - - -#draw from a standard Gaussian 9 times +# Draw from a standard Gaussian 9 times rnorm(9) # [1] 0.07528471 1.03499859 1.34809556 -0.82356087 0.61638975 -1.88757271 # [7] -0.59975593 0.57629164 1.08455362 - - - - - - -######################### -# Basic programming stuff -######################### - -# NUMBERS - -# "numeric" means double-precision floating-point numbers -5 # 5 -class(5) # "numeric" -5e4 # 50000 #handy when dealing with large,small,or variable orders of magnitude -6.02e23 # Avogadro's number -1.6e-35 # Planck length - -# long-storage integers are written with L -5L # 5 -class(5L) # "integer" - -# Try ?class for more information on the class() function -# In fact, you can look up the documentation on `xyz` with ?xyz -# or see the source for `xyz` by evaluating xyz - -# Arithmetic -10 + 66 # 76 -53.2 - 4 # 49.2 -2 * 2.0 # 4 -3L / 4 # 0.75 -3 %% 2 # 1 - -# Weird number types -class(NaN) # "numeric" +################################################## +# 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 class(Inf) # "numeric" -class(-Inf) # "numeric" #used in for example integrate( dnorm(x), 3, Inf ) -- which obviates Z-score tables - -# but beware, NaN isn't the only weird type... -class(NA) # see below -class(NULL) # NULL - - -# SIMPLE LISTS -c(6, 8, 7, 5, 3, 0, 9) # 6 8 7 5 3 0 9 -c('alef', 'bet', 'gimmel', 'dalet', 'he') # "alef" "bet" "gimmel" "dalet" "he" -c('Z', 'o', 'r', 'o') == "Zoro" # FALSE FALSE FALSE FALSE - -#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) -# [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 - -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" - - -# Access the n'th element of a list with list.name[n] or sometimes list.name[[n]] -letters[18] # "r" -LETTERS[13] # "M" -month.name[9] # "September" -c(6, 8, 7, 5, 3, 0, 9)[3] # 7 - - +class(-Inf) # "numeric" +# You might use "Inf", for example, in integrate( dnorm(x), 3, Inf); +# 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 +3L / 4 # 0.75 # integer over integer gives numeric +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 # CHARACTERS - # There's no difference between strings and characters in R - -"Horatio" # "Horatio" +"Horatio" # "Horatio" class("Horatio") # "character" -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." - - +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" # LOGICALS - -# booleans +# In R, a "logical" is a boolean class(TRUE) # "logical" class(FALSE) # "logical" -# Behavior is normal +# Their behavior is normal TRUE == TRUE # TRUE TRUE == FALSE # FALSE FALSE != FALSE # FALSE FALSE != TRUE # TRUE # Missing data (NA) is logical, too class(NA) # "logical" - - +# 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 # FACTORS - # The factor class is for categorical data -# which can be ordered (like childrens' grade levels) -# or unordered (like gender) -levels(factor(c("female", "male", "male", "female", "NA", "female"))) # "female" "male" "NA" - +# Factors can be ordered (like childrens' grade levels) or unordered (like gender) factor(c("female", "female", "male", "NA", "female")) # female female male NA female # Levels: female male NA +# The "levels" are the values the categorical data can take +levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male" "NA" +# If a factor has length 1, its levels will have length 1, too +length(factor("male")) # 1 +length(levels(factor("male"))) # 1 +# Factors are commonly seen in data frames, a data structure we will cover later +# in this tutorial: +data(infert) # "Infertility after Spontaneous and Induced Abortion" +levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" + +# WEIRD TYPES +# A quick summary of some of the weirder types in R +class(Inf) # "numeric" +class(-Inf) # "numeric" +class(NaN) # "numeric" +class(NA) # "logical" +class(NULL) # NULL -data(infert) #Infertility after Spontaneous and Induced Abortion -levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" +# 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 +# If you put elements of different classes into a vector, weird coercions happen: +c(TRUE, 4) # 1 4 +c("dog", TRUE, 4) # "dog" "TRUE" "4" +as.numeric("Bilbo") +# => +# [1] NA +# Warning message: +# NAs introduced by coercion +# Also note: those were just the basic data types +# There are many more data types, such as for dates, time series, etc. -# VARIABLES -# Lots of way to assign stuff +################################################## +# 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: x = 5 # this is possible y <- "1" # this is preferred TRUE -> z # this works but is weird -# We can use coerce variables to different classes -as.numeric(y) # 1 -as.character(x) # "5" - # 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 # IF/ELSE - # Again, pretty standard if (4 > 3) { - print("Huzzah! It worked!") + print("4 is greater than 3") } else { - print("Noooo! This is blatantly illogical!") + print("4 is not greater than 3") } # => -# [1] "Huzzah! It worked!" +# [1] "4 is greater than 3" # FUNCTIONS - # Defined like so: jiggle <- function(x) { x = x + rnorm(1, sd=.1) #add in a bit of (controlled) noise return(x) } - # Called like any other R function: jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043 -######################### -# Fun with data: vectors, matrices, data frames, and arrays -######################### + + +########################################################################### +# Data structures: Vectors, matrices, data frames, and arrays +########################################################################### # ONE-DIMENSIONAL -# You can vectorize anything, so long as all components have the same type +# Let's start from the very beginning, and with something you already know: vectors. +# As explained above, every single element in R is already a vector +# Make sure the elements of long vectors all have the same type vec <- c(8, 9, 10, 11) vec # 8 9 10 11 -# The class of a vector is the class of its components -class(vec) # "numeric" -# If you vectorize items of different classes, weird coercions happen -c(TRUE, 4) # 1 4 -c("dog", TRUE, 4) # "dog" "TRUE" "4" - -# We ask for specific components like so (R starts counting from 1) -vec[1] # 8 +# 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 # We can also search for the indices of specific components, which(vec %% 2 == 0) # 1 3 -# or grab just the first or last entry in the vector +# grab just the first or last entry in the vector, head(vec, 1) # 8 tail(vec, 1) # 11 +# or figure out if a certain value is in the vector +any(vec == 10) # TRUE # If an index "goes over" you'll get NA: vec[6] # NA # You can find the length of your vector with length() length(vec) # 4 - # You can perform operations on entire vectors or subsets of vectors vec * 4 # 16 20 24 28 vec[2:3] * 5 # 25 30 +any(vec[2:3] == 8) # FALSE # and there are many built-in functions to summarize vectors mean(vec) # 9.5 var(vec) # 1.666667 -sd(vec) # 1.290994 +sd(vec) # 1.290994 max(vec) # 11 min(vec) # 8 sum(vec) # 38 +# 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) +# [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 # TWO-DIMENSIONAL (ALL ONE CLASS) @@ -361,6 +389,7 @@ mat[1,] # 1 4 3 * mat[,1] # 3 6 9 # Ask for a specific cell mat[3,2] # 6 + # Transpose the whole matrix t(mat) # => @@ -368,6 +397,14 @@ t(mat) # [1,] 1 2 3 # [2,] 4 5 6 +# Matrix multiplication +mat %*% t(mat) +# => +# [,1] [,2] [,3] +# [1,] 17 22 27 +# [2,] 22 29 36 +# [3,] 27 36 45 + # cbind() sticks vectors together column-wise to make a matrix mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog")) mat2 @@ -395,24 +432,85 @@ mat3 # TWO-DIMENSIONAL (DIFFERENT CLASSES) # For columns of different classes, use the data frame -dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog")) -names(dat) <- c("number", "species") # name the columns -class(dat) # "data.frame" -dat +# 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 # => -# number species -# 1 5 dog -# 2 2 cat -# 3 1 bird -# 4 4 dog -class(dat$number) # "numeric" -class(dat[,2]) # "factor" +# 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 # The data.frame() function converts character vectors to factor vectors +# by default; turn this off by setting stringsAsFactors = FALSE when +# you create the data.frame +?data.frame # There are many twisty ways to subset data frames, all subtly unalike -dat$number # 5 2 1 4 -dat[,1] # 5 2 1 4 -dat[,"number"] # 5 2 1 4 +students$year # 3 2 2 1 0 -1 +students[,2] # 3 2 2 1 0 -1 +students[,"year"] # 3 2 2 1 0 -1 + +# A popular replacement for the data.frame structure is the data.table +# 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: +install.packages("data.table") +require(data.table) +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 +students[name=="Ginny"] +# => +# name year house +# 1: Ginny -1 G +students[year==2] +# => +# name year house +# 1: Fred 2 G +# 2: George 2 G +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) +students <- founders[students] # merge the two data sets +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 # MULTI-DIMENSIONAL (ALL OF ONE CLASS) @@ -446,15 +544,23 @@ 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)) list1 <- list(time = 1:40) list1$price = c(rnorm(40,.5*list1$time,4)) # random list1 - # You can get items in the list like so -list1$time -# You can subset list items like vectors +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 list1$price[4] -######################### +# 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 + +################################################## # The apply() family of functions -######################### +################################################## # Remember mat? mat @@ -467,7 +573,7 @@ mat # 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 -apply(mat, MAR = 2, myFunc) +apply(mat, MAR = 2, jiggle) # => # [,1] [,2] # [1,] 3 15 @@ -478,16 +584,18 @@ apply(mat, MAR = 2, myFunc) # Don't feel too intimidated; everyone agrees they are rather confusing # The plyr package aims to replace (and improve upon!) the *apply() family. - install.packages("plyr") require(plyr) ?plyr + + ######################### # Loading data ######################### # "pets.csv" is a file on the internet +# (but it could just as easily be be a file on your own computer) pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv") pets head(pets, 2) # first two rows @@ -499,10 +607,13 @@ write.csv(pets, "pets2.csv") # to make a new .csv file # Try ?read.csv and ?write.csv for more information + + ######################### # Plots ######################### +# BUILT-IN PLOTTING FUNCTIONS # Scatterplots! plot(list1$time, list1$price, main = "fake data") # Regressions! @@ -512,18 +623,25 @@ linearModel # outputs result of regression abline(linearModel, col = "red") # Get a variety of nice diagnostics plot(linearModel) - # 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")) +# GGPLOT2 +# But these are not even the prettiest of R's plots # Try the ggplot2 package for more and better graphics - install.packages("ggplot2") require(ggplot2) ?ggplot2 +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/) + + ``` -- cgit v1.2.3 From a8d8cee0d82a8f8be48f3a80c307223856764a31 Mon Sep 17 00:00:00 2001 From: e99n09 Date: Sat, 24 May 2014 08:54:39 -0400 Subject: Update r.html.markdown Minor changes to comments (fixing typos, etc.). Deleted "weird types" section; broke out "NULL" type into its own type category. Added instructions for dropping rows and columns in data.frame and data.table. How to make summary tables in data.table. --- r.html.markdown | 93 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 24 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index dfc945c1..cd09e8da 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -188,7 +188,7 @@ class(-Inf) # "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 -3L / 4 # 0.75 # integer over integer gives numeric +3L / 4 # 0.75 # integer over numeric gives numeric 3 %% 2 # 1 # the remainder of two numerics is another numeric # Illegal arithmetic yeilds you a "not-a-number": 0 / 0 # NaN @@ -241,27 +241,29 @@ factor(c("female", "female", "male", "NA", "female")) # Levels: female male NA # The "levels" are the values the categorical data can take levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male" "NA" -# If a factor has length 1, its levels will have length 1, too +# If a factor vector has length 1, its levels will have length 1, too length(factor("male")) # 1 length(levels(factor("male"))) # 1 # Factors are commonly seen in data frames, a data structure we will cover later -# in this tutorial: data(infert) # "Infertility after Spontaneous and Induced Abortion" levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" -# WEIRD TYPES -# A quick summary of some of the weirder types in R -class(Inf) # "numeric" -class(-Inf) # "numeric" -class(NaN) # "numeric" -class(NA) # "logical" +# NULL +# "NULL" is a weird one; use it to "blank out" a vector class(NULL) # NULL +parakeet +# => +# [1] "beak" "feathers" "wings" "eyes" +parakeet <- NULL +parakeet +# => +# NULL # 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 -# If you put elements of different classes into a vector, weird coercions happen: +# If you put elements of different types into a vector, weird coercions happen: c(TRUE, 4) # 1 4 c("dog", TRUE, 4) # "dog" "TRUE" "4" as.numeric("Bilbo") @@ -332,8 +334,6 @@ jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043 # ONE-DIMENSIONAL # Let's start from the very beginning, and with something you already know: vectors. -# As explained above, every single element in R is already a vector -# Make sure the elements of long vectors all have the same type vec <- c(8, 9, 10, 11) vec # 8 9 10 11 # We ask for specific elements by subsetting with square brackets @@ -345,9 +345,9 @@ month.name[9] # "September" c(6, 8, 7, 5, 3, 0, 9)[3] # 7 # We can also search for the indices of specific components, which(vec %% 2 == 0) # 1 3 -# grab just the first or last entry in the vector, +# grab just the first or last few entries in the vector, head(vec, 1) # 8 -tail(vec, 1) # 11 +tail(vec, w) # 10 11 # or figure out if a certain value is in the vector any(vec == 10) # TRUE # If an index "goes over" you'll get NA: @@ -358,7 +358,7 @@ length(vec) # 4 vec * 4 # 16 20 24 28 vec[2:3] * 5 # 25 30 any(vec[2:3] == 8) # FALSE -# and there are many built-in functions to summarize vectors +# and R has many built-in functions to summarize vectors mean(vec) # 9.5 var(vec) # 1.666667 sd(vec) # 1.290994 @@ -368,6 +368,7 @@ sum(vec) # 38 # 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) +# => # [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 @@ -427,11 +428,11 @@ mat3 # [,1] [,2] [,3] [,4] # [1,] 1 2 4 5 # [2,] 6 7 0 4 -# Aah, everything of the same class. No coercions. Much better. +# Ah, everything of the same class. No coercions. Much better. # TWO-DIMENSIONAL (DIFFERENT CLASSES) -# For columns of different classes, use the data frame +# For columns of different types, use a data frame # This data structure is so useful for statistical programming, # a version of it was added to Python in the package "pandas". @@ -465,11 +466,11 @@ students$year # 3 2 2 1 0 -1 students[,2] # 3 2 2 1 0 -1 students[,"year"] # 3 2 2 1 0 -1 -# A popular replacement for the data.frame structure is the data.table +# An augmented version of the data.frame structure is the data.table # 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: -install.packages("data.table") -require(data.table) +install.packages("data.table") # download the package from CRAN +require(data.table) # load it students <- as.data.table(students) students # note the slightly different print-out # => @@ -480,15 +481,17 @@ students # note the slightly different print-out # 4: Cho 1 R # 5: Draco 0 S # 6: Ginny -1 G -students[name=="Ginny"] +students[name=="Ginny"] # get rows with name == "Ginny" # => # name year house # 1: Ginny -1 G -students[year==2] +students[year==2] # get rows with year == 2 # => # name year house # 1: Fred 2 G # 2: George 2 G +# data.table makes merging two data sets easy +# let's make another data.table to merge with students founders <- data.table(house=c("G","H","R","S"), founder=c("Godric","Helga","Rowena","Salazar")) founders @@ -500,7 +503,7 @@ founders # 4: S Salazar setkey(students, house) setkey(founders, house) -students <- founders[students] # merge the two data sets +students <- founders[students] # merge the two data sets by matching "house" setnames(students, c("house","houseFounderName","studentName","year")) students[,order(c("name","year","house","houseFounderName")), with=F] # => @@ -512,9 +515,51 @@ students[,order(c("name","year","house","houseFounderName")), with=F] # 5: Cho 1 R Rowena # 6: Draco 0 S Salazar -# MULTI-DIMENSIONAL (ALL OF ONE CLASS) +# data.table makes summary tables easy +# => +# students[,sum(year),by=house] +# 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) # Arrays creates n-dimensional tables +# All elements must be of the same type # 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)) # => -- cgit v1.2.3 From ae443092709b90594c86552645dffa1a767d12f0 Mon Sep 17 00:00:00 2001 From: e99n09 Date: Sat, 24 May 2014 13:11:07 -0400 Subject: Fix accidentally-commented-out line Un-commented a line that should have been executable. --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index cd09e8da..f6a62ae6 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -516,8 +516,8 @@ students[,order(c("name","year","house","houseFounderName")), with=F] # 6: Draco 0 S Salazar # data.table makes summary tables easy +students[,sum(year),by=house] # => -# students[,sum(year),by=house] # house V1 # 1: G 3 # 2: H 3 -- cgit v1.2.3 From 101a75fcbc0b12b6ad7957d6fa721f8828635965 Mon Sep 17 00:00:00 2001 From: Antonio Lima Date: Thu, 29 May 2014 16:51:24 +0200 Subject: Minor typo --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index f6a62ae6..b59fc29c 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -347,7 +347,7 @@ c(6, 8, 7, 5, 3, 0, 9)[3] # 7 which(vec %% 2 == 0) # 1 3 # grab just the first or last few entries in the vector, head(vec, 1) # 8 -tail(vec, w) # 10 11 +tail(vec, 2) # 10 11 # or figure out if a certain value is in the vector any(vec == 10) # TRUE # If an index "goes over" you'll get NA: -- cgit v1.2.3 From eab554a7a7f2869ff7dac9f54acce9a7ed55cfa4 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 8 Sep 2014 13:08:28 +0200 Subject: Review docs for added rouge lexers and update those with new highlighters --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index b59fc29c..7cb56fd7 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -8,7 +8,7 @@ filename: learnr.r 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. -```python +```r # Comments start with number symbols. -- cgit v1.2.3 From a32bb0a2cc3b53b4ffa4d2e93ebe664afb7a7729 Mon Sep 17 00:00:00 2001 From: Bruno Kim Medeiros Cesar Date: Sat, 1 Nov 2014 01:37:26 -0200 Subject: Remove misleading example of NA in Levels section The levels section was using a level "NA" together with "female" and "male", which is misleading (given that NA was just introduced as a missing value). I updated it to what I believe is a better example of how NAs are interpreted. Additionally, I edited a comment about Inf that wouldn't compile. How about inserting it at the tutorial altogether? --- r.html.markdown | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 7cb56fd7..c555d748 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -179,7 +179,7 @@ c(3,3,3,2,2,1) # 3 3 3 2 2 1 # You can also have infinitely large or small numbers class(Inf) # "numeric" class(-Inf) # "numeric" -# You might use "Inf", for example, in integrate( dnorm(x), 3, Inf); +# You might use "Inf", for example, in integrate(dnorm, 3, Inf); # this obviates Z-score tables. # BASIC ARITHMETIC @@ -236,11 +236,12 @@ c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE # FACTORS # The factor class is for categorical data # Factors can be ordered (like childrens' grade levels) or unordered (like gender) -factor(c("female", "female", "male", "NA", "female")) -# female female male NA female -# Levels: female male NA +factor(c("female", "female", "male", NA, "female")) +# female female male female +# Levels: female male # The "levels" are the values the categorical data can take -levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male" "NA" +# Note that missing data does not enter the levels +levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male" # If a factor vector has length 1, its levels will have length 1, too length(factor("male")) # 1 length(levels(factor("male"))) # 1 -- cgit v1.2.3 From 209dc039ecc5ee280e5239afe5e2a77a851f795f Mon Sep 17 00:00:00 2001 From: searoso Date: Tue, 20 Jan 2015 21:34:18 +0300 Subject: Update r.html.markdown Added logical operators that were missing. --- r.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index c555d748..13fa2654 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -229,6 +229,13 @@ FALSE != FALSE # FALSE FALSE != TRUE # TRUE # Missing data (NA) is logical, too class(NA) # "logical" +# Use for | and & for logic operations. +# OR +TRUE | FALSE # TRUE +# AND +TRUE & FALSE # FALSE +# You can test if x is TRUE +isTRUE(TRUE) # TRUE # 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 -- cgit v1.2.3 From 3e40335e96eca14affc8b9ad70bbda1c90e09b62 Mon Sep 17 00:00:00 2001 From: searoso Date: Sun, 25 Jan 2015 05:06:13 +0300 Subject: Update r.html.markdown --- r.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 13fa2654..447db4b3 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -229,7 +229,7 @@ FALSE != FALSE # FALSE FALSE != TRUE # TRUE # Missing data (NA) is logical, too class(NA) # "logical" -# Use for | and & for logic operations. +# Use | and & for logic operations. # OR TRUE | FALSE # TRUE # AND -- cgit v1.2.3 From 2f8765033fa2fcbe01ce535c314b71d50d29fb47 Mon Sep 17 00:00:00 2001 From: acganesh Date: Wed, 28 Jan 2015 23:27:09 -0600 Subject: Add definition of parakeet in r.html.markdown Added a definition of the `parakeet` variable. It's currently never defined, but referenced. --- r.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index 447db4b3..d3d725d3 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -259,6 +259,7 @@ levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" # NULL # "NULL" is a weird one; use it to "blank out" a vector class(NULL) # NULL +parakeet = c("beak", "feathers", "wings", "eyes") parakeet # => # [1] "beak" "feathers" "wings" "eyes" -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- r.html.markdown | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'r.html.markdown') diff --git a/r.html.markdown b/r.html.markdown index d3d725d3..93751df5 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -36,8 +36,8 @@ head(rivers) # peek at the data set length(rivers) # how many rivers were measured? # 141 summary(rivers) # what are some summary statistics? -# Min. 1st Qu. Median Mean 3rd Qu. Max. -# 135.0 310.0 425.0 591.2 680.0 3710.0 +# Min. 1st Qu. Median Mean 3rd Qu. Max. +# 135.0 310.0 425.0 591.2 680.0 3710.0 # make a stem-and-leaf plot (a histogram-like data visualization) stem(rivers) @@ -54,14 +54,14 @@ stem(rivers) # 14 | 56 # 16 | 7 # 18 | 9 -# 20 | +# 20 | # 22 | 25 # 24 | 3 -# 26 | -# 28 | -# 30 | -# 32 | -# 34 | +# 26 | +# 28 | +# 30 | +# 32 | +# 34 | # 36 | 1 stem(log(rivers)) # Notice that the data are neither normal nor log-normal! @@ -70,7 +70,7 @@ stem(log(rivers)) # Notice that the data are neither normal nor log-normal! # The decimal point is 1 digit(s) to the left of the | # # 48 | 1 -# 50 | +# 50 | # 52 | 15578 # 54 | 44571222466689 # 56 | 023334677000124455789 @@ -85,7 +85,7 @@ stem(log(rivers)) # Notice that the data are neither normal nor log-normal! # 74 | 84 # 76 | 56 # 78 | 4 -# 80 | +# 80 | # 82 | 2 # make a histogram: @@ -108,7 +108,7 @@ sort(discoveries) # [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) -# +# # The decimal point is at the | # # 0 | 000000000 @@ -122,14 +122,14 @@ stem(discoveries, scale=2) # 8 | 0 # 9 | 0 # 10 | 0 -# 11 | +# 11 | # 12 | 0 max(discoveries) # 12 summary(discoveries) -# Min. 1st Qu. Median Mean 3rd Qu. Max. -# 0.0 2.0 3.0 3.1 4.0 12.0 +# Min. 1st Qu. Median Mean 3rd Qu. Max. +# 0.0 2.0 3.0 3.1 4.0 12.0 # Roll a die a few times round(runif(7, min=.5, max=6.5)) @@ -262,7 +262,7 @@ class(NULL) # NULL parakeet = c("beak", "feathers", "wings", "eyes") parakeet # => -# [1] "beak" "feathers" "wings" "eyes" +# [1] "beak" "feathers" "wings" "eyes" parakeet <- NULL parakeet # => @@ -279,7 +279,7 @@ as.numeric("Bilbo") # => # [1] NA # Warning message: -# NAs introduced by coercion +# NAs introduced by coercion # Also note: those were just the basic data types # There are many more data types, such as for dates, time series, etc. @@ -419,10 +419,10 @@ mat %*% t(mat) mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog")) mat2 # => -# [,1] [,2] -# [1,] "1" "dog" -# [2,] "2" "cat" -# [3,] "3" "bird" +# [,1] [,2] +# [1,] "1" "dog" +# [2,] "2" "cat" +# [3,] "3" "bird" # [4,] "4" "dog" class(mat2) # matrix # Again, note what happened! -- cgit v1.2.3