Code from Appendix B in an R Notebook.

3 + 6
[1] 9
2 + 3
[1] 5
x <- c(4,5,3,2)
x
[1] 4 5 3 2
y <- seq(1,4)
y
[1] 1 2 3 4
mean(x)
[1] 3.5
sd(y)
[1] 1.290994

Reading in data from a website.

ds <- read.csv("http://nhorton.people.amherst.edu/r2/datasets/help.csv")
ds
mean(ds$age)
[1] 35.65342
with(ds,mean(age))
[1] 35.65342
ds$age[1:15]
 [1] 37 37 26 39 32 47 49 28 50 39 34 58 58 60 36

Functions.

help(mean)
example(mean)

mean> x <- c(0:10, 50)

mean> xm <- mean(x)

mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50

Vectors

x <- c(5, 7, 9, 13, -4, 8)
x
[1]  5  7  9 13 -4  8
x[2]
[1] 7
x[c(2,3)]
[1] 7 9
x[1:5]
[1]  5  7  9 13 -4
x[-6]
[1]  5  7  9 13 -4

Lists

newlist <- list(first = "hello", second = 42, Bob = TRUE)
is.list(newlist)
[1] TRUE
newlist
$first
[1] "hello"

$second
[1] 42

$Bob
[1] TRUE
newlist$first
[1] "hello"

Matricies

A <- matrix(x, 2, 3)
is.matrix(A)
[1] TRUE
A
     [,1] [,2] [,3]
[1,]    5    9   -4
[2,]    7   13    8
A[2,3]
[1] 8

Dataframes

y <- rep(11, length(x))
y
[1] 11 11 11 11 11 11
ds <- data.frame(x,y)
ds
is.data.frame(ds)
[1] TRUE

Tidyverse tibble

library(tidyverse)
tbl <- as.tibble(ds)
class(tbl)
[1] "tbl_df"     "tbl"        "data.frame"
tbl

Functions

vals <- rnorm(1000)
quantile(vals)
          0%          25%          50%          75%         100% 
-3.416958272 -0.691014809  0.002166823  0.623659938  3.001089214 

Useful function to see what version of R you are running and what packages are loaded.

sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.3.0   stringr_1.3.1   dplyr_0.7.7     purrr_0.2.5     readr_1.1.1    
[6] tidyr_0.8.2     tibble_1.4.2    ggplot2_3.1.0   tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1       cellranger_1.1.0 pillar_1.3.0     compiler_3.4.4  
 [5] plyr_1.8.4       bindr_0.1.1      tools_3.4.4      digest_0.6.18   
 [9] evaluate_0.12    jsonlite_1.5     lubridate_1.7.4  nlme_3.1-137    
[13] gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.2  rlang_0.3.0.1   
[17] cli_1.0.1        rstudioapi_0.8   yaml_2.2.0       haven_1.1.2     
[21] bindrcpp_0.2.2   withr_2.1.2      xml2_1.2.0       httr_1.3.1      
[25] knitr_1.20       hms_0.4.2        rprojroot_1.3-2  grid_3.4.4      
[29] tidyselect_0.2.5 glue_1.3.0       R6_2.3.0         readxl_1.1.0    
[33] rmarkdown_1.10   modelr_0.1.2     magrittr_1.5     htmltools_0.3.6 
[37] backports_1.1.2  scales_1.0.0     rsconnect_0.8.8  rvest_0.3.2     
[41] assertthat_0.2.0 colorspace_1.3-2 labeling_0.3     stringi_1.2.4   
[45] lazyeval_0.2.1   munsell_0.5.0    broom_0.5.0      crayon_1.3.4    
LS0tCnRpdGxlOiAiR2V0dGluZyBzdGFydGVkIHdpdGggUiIKYXV0aG9yOiAiUHJvZi4gRXJpYyBBLiBTdWVzcyIKb3V0cHV0OgogIHdvcmRfZG9jdW1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