R Notes

Table of Contents

Vectors

c is for combine

sentence <- c("this", "is", "a", "sentence")
sentence                                     # [1] "this" "is" "a" "sentence"

First index is 1, not 0

sentence[1] # "this"

Overwriting

sentence[4] <- "silly"
sentence               # [1] "this" "is" "a" "silly"

Appending

sentence[5] <- "example"
sentence                 # [1] "this" "is" "a" "silly" "example"

Getting multiple elements

sentence[c(1, 5)] # [1] "this" "example"

Ranges

sentence[4:5]                          # [1] "silly" "example"
sentence[4:5] <- c("cool", "language") # [1] "this" "is" "a" "cool" "language"

Sequences

5:9 == seq(5, 9) # [1] TRUE TRUE TRUE TRUE TRUE

With a step

seq(2, 8, 2)     # [1] 2 4 6 8

Named elements

named <- 1:3
names(named) <- c("this", "that", "the other")
named                                          # this      that the other
                                               #    1         2         3
named["the other"] <- 42
named["the other"] == named[3]                 # T
named[3] == 42                                 # T

Combining

sails <- c(1:10,5:1,1:30,15:1,1:20,10:1)
barplot(sails)

barplot_01.png

Basic arithmetic

misfits <- c(1, 3, 8) # [1] 1 3 8
misfits + 10          # [1] 11 13 18
misfits - 10          # [1] -9 -7 -2
misfits * 10          # [1] 10 30 80
misfits / 10          # [1] 0.1 0.3 0.8

Sort of like Clojure's map.

(for [f [+ - * /]] (map #(f % 10) '(1 3 8)))

Keep in mind, it doesn't alter the original variable.

misfits # [1] 1 3 8

Scatter plots

x <-

na.rm

a <- c(1, NA, 2, NA, 3) # [1] 1 NA 2 NA 3
sum(a)                  # [1] NA
sum(a, na.rm = TRUE)    # [1] 6

Links

Generated by Eric Bailey on 2017-03-20 Mon 06:26 using Emacs 25.1.1 (Org mode 9.0.5).