summaryrefslogtreecommitdiffhomepage
path: root/r.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'r.html.markdown')
-rw-r--r--r.html.markdown36
1 files changed, 20 insertions, 16 deletions
diff --git a/r.html.markdown b/r.html.markdown
index e19eaeb8..2cf63288 100644
--- a/r.html.markdown
+++ b/r.html.markdown
@@ -2,21 +2,23 @@
language: R
author: e99n09
author_url: http://github.com/e99n09
-
+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
+```python
# Comments start with hashtags.
# 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
+
+#########################
# The absolute basics
-###################################################################################
+#########################
# NUMBERS
@@ -122,9 +124,9 @@ myFunc <- function(x) {
# Called like any other R function:
myFunc(5) # => [1] 19
-###################################################################################
+#########################
# Fun with data: vectors, matrices, data frames, and arrays
-###################################################################################
+#########################
# ONE-DIMENSIONAL
@@ -133,7 +135,7 @@ vec <- c(8, 9, 10, 11)
vec # => [1] 8 9 10 11
# The class of a vector is the class of its components
class(vec) # => [1] "numeric"
-# If you vectorize items of different classes, weird coersions happen
+# If you vectorize items of different classes, weird coercions happen
c(TRUE, 4) # => [1] 1 4
c("dog", TRUE, 4) # => [1] "dog" "TRUE" "4"
@@ -258,7 +260,7 @@ 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))
# LISTS (MULTI-DIMENSIONAL, POSSIBLY RAGGED, OF DIFFERENT TYPES)
# Finally, R has lists (of vectors)
-list1 <- list(time = 1:40, price = c(rnorm(40,.5*list1$time,4))) # generate random
+list1 <- list(time = 1:40, price = c(rnorm(40,.5*list1$time,4))) # random
list1
# You can get items in the list like so
@@ -266,9 +268,9 @@ list1$time
# You can subset list items like vectors
list1$price[4]
-###################################################################################
+#########################
# The apply() family of functions
-###################################################################################
+#########################
# Remember mat?
mat
@@ -289,15 +291,17 @@ apply(mat, MAR = 2, myFunc)
# [3,] 11 23
# Other functions: ?lapply, ?sapply
+# 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
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
@@ -306,14 +310,14 @@ head(pets, 2) # first two rows
tail(pets, 1) # last row
# To save a data frame or matrix as a .csv file
-write.csv(pets, "pets2.csv") # to make a new .csv file in the working directory
+write.csv(pets, "pets2.csv") # to make a new .csv file
# set working directory with setwd(), look it up with getwd()
# Try ?read.csv and ?write.csv for more information
-###################################################################################
-# Plots and tests
-###################################################################################
+#########################
+# Plots
+#########################
# Scatterplots!
plot(list1$time, list1$price, main = "fake data")