Import (data) ➡️ Tidy (data) ➡️ Visualize (data) ➡️ Communicate (findings)
| Term | Meaning |
|---|---|
| Reproducibility | Same data, same methods |
| Replicability | New data, same methods |
Most scientists report failing to reproduce others’ results, and many their own. Survey of 1,576 researchers (Baker, 2016, *Nature*).
By the end of this session, you should be able to:
# install.packages("dplyr") # This call installs a package
# library(dplyr) # This loads a package
# devtools packages
# install.packages("devtools")
# install_github("username/repositoryname")
2 + 2 # add
## [1] 4
3 - 2 # subtract
## [1] 1
3 * 3 # multiply
## [1] 9
4 / 2 # division
## [1] 2
sqrt(9) # square root
## [1] 3
# c() is a common function for concatenating things together
c(1:10)
## [1] 1 2 3 4 5 6 7 8 9 10
# You can then recall the values (object) associated with your variable...
my_variable <- c(1:10)
myAlphabet <- letters[1:5]
myNumbers <- c(1:5)
#And plug it into functions, ie, do computations on it...
my_variable
## [1] 1 2 3 4 5 6 7 8 9 10
my_variable * 2
## [1] 2 4 6 8 10 12 14 16 18 20
# camel case
myNumber <- "Hello" # bad naming
myNumber <- 10 # good naming
# skeleton case
my_number <- 10
typeof(), will tell you what data type you
have…boolean data are generally referred to as logicalmyBool <- TRUE # this is camel case
typeof(myBool)
## [1] "logical"
R numeric data are subdivided into double and integermyDouble <- 10.5
typeof(myDouble)
## [1] "double"
myInteger <- 10L
typeof(myInteger)
## [1] "integer"
character data always needs to be wrapped in single or double
quotations.myChar <- "a" # Note that R does not distinguish between '' and "" but the use has to match
typeof(myChar)
## [1] "character"
myComplexNum <- 9 + 3i # remember sqrt(-1) :)
typeof(myComplexNum)
## [1] "complex"
# numeric vector
c(1:10)
## [1] 1 2 3 4 5 6 7 8 9 10
# character vector
c("a", "b", "c")
## [1] "a" "b" "c"
# logical vector
c(TRUE, TRUE, FALSE)
## [1] TRUE TRUE FALSE
# note that vectors do not allow you to mix types
# c(TRUE, 10) # this will convert TRUE to 1
# c(TRUE, "10") # this will convert TRUE to "TRUE"
matrix(1:10, nrow = 2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
data.frame(letter = letters[1:5],
nol = 1:5) # nol mean number of letter
## letter nol
## 1 a 1
## 2 b 2
## 3 c 3
## 4 d 4
## 5 e 5
list(item_1 = myAlphabet,
item_2 = c(1:3))
## $item_1
## [1] "a" "b" "c" "d" "e"
##
## $item_2
## [1] 1 2 3
my_array <- array(1:12, dim = c(2, 3, 2)) # 2 rows, 3 columns, 2 layers
my_array
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
## [2,] 8 10 12
class(c(1:10))
## [1] "integer"
class(matrix(1:10, ncol = 2))
## [1] "matrix" "array"
class(data.frame(var_1 = letters,var_2 = 1:26))
## [1] "data.frame"
class(list(item_1 = letters,item_2 = 1:10,item_3 = c(TRUE, FALSE)))
## [1] "list"
swirl, which teaches R interactively in your console? operator (and vignettes)?sqrt # help page for a function
??regression # search all help pages for a topic
browseVignettes("dplyr") # long-form guides for a package
And through the library, the O’Reilly platform hosts a plethora of titles related to R.