- Access data
- Build models
- Test hypotheses
- Run analyses
- Report the results
- Share the results and the techniques with others
?formula
?factor
?ordered
?levels
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
?data.frame
?read.csv
?read.table
vec <- c(1, 2, 3)
vec[vec > 2]
## [1] 3
vec <- 1:3
vec > 2
## [1] FALSE FALSE TRUE
vec <- seq(from = 1, to = 3)
subs <- c(T, F, T)
vec[subs]
## [1] 1 3
df <- data.frame(val = c(1, 2, 3),
other = c("a", "b", "a"))
df$other
## [1] "a" "b" "a"
df[df$other == "b", ]
## val other
## 2 2 b
subset(df, val == 1)
## val other
## 1 1 a
But these things are constantly evolving:
library(dplyr)
df %>% filter(other == "b")
## val other
## 1 2 b
mtcars %>% filter(mpg > 20) %>% filter(carb >= 4)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
plot(c(1, 2, 3), c(1, 2, 1))
lines(c(1, 2, 3), c(2, 1, 2))
Again, there are much more sophisticated ways, as you know… e.g.
library(ggplot2)
mtcars %>% ggplot(aes(wt, mpg)) + geom_point() + geom_smooth()
png("out.png")
plot(c(1, 2, 3), c(1, 2, 1), type = "l")
dev.off()
?pdf
?png
?Devices
Again, using ggplot2…
g <- mtcars %>% ggplot(aes(wt, mpg)) + geom_point() + geom_smooth()
ggsave("out.png", g)
?ggsave
Install it first:
install.packages(c("tibble"))
or from RStudio menus, and then use it:
library(tibble)
not forgetting:
update.packages()
occasionally.
Learn how to:
- Write code
- Write documentation
- So you can come back and understand it later
- Identify and isolate reusable code
- So you don’t have to write it again later
- Generate documented results
- Read and understand other people’s code
“I honestly didn’t think you could even USE emoji in variable names. Or that there were so many different crying ones.”
- Read and write well-documented and well-structured code
- Use this to generate clear and well-documented results/reports
- Problems will be structured around population dynamics, epidemiology and biodiversity
- They will start off easy and ramp up
- Packages (libraries) will allow you to solve many problems easily, but we are focusing on problems they don’t help with!
and optionally: