owned this note
owned this note
Published
Linked with GitHub
---
tags: gps
---
# 2020-GPS-Data-Skills-Course-Intro-to-R
## Course website: https://ucsdlib.github.io/win2020-gps-r/
## This HackMD: https://hackmd.io/@U2NG/BySbi0KjS
# We will use collaborative notes!
Hello! Hi! Nice to meet you!
# Please Sign In here
sign in moved to spreadsheet.
## Download data:
in your web browser right click and select `Save As`: `gapminder.csv`
change the extension to `.csv`
<https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv>
You can use R as a calculator
#### loading packages:
```
install.packages("ggplot2")
library(ggplot2)
```
#Math
1+100
3+5*2
(3+5)*2
sin(1)
log(10)
?log
#Comparisons
1==1
1==2
1!=2
1<2
1>0
1>=1
# Variable Assignment
x <- 1/40
x<-100
x <- x + 1
x
1:5
2^(1:5)
x<- 1:5
c(1,3,5,7)
y<- c(1,3,5,7)
### Environment
ls()
rm(x)
## Packages
```
install.packages("knitr")
install.packages("ggplot2")
install.packages("gridExtra")
```
## load
```
library(knitr)
library(ggplot2)
library(gridExtra)
```
To convert a variable to a factor:
```
as.factor(variable-name)
```
To convert a factored variable to character:
```
as.character(factored-variable-name)
```
To convert a factored variable to a number
```
as.numeric(factored-variable-name)
```
#### Gapminder information:
```
gapminder$continent
unique(gapminder$continent)
length(gapminder$continent)
summary(gapminder$lifeExp)
```
### Subsetting:
```
head(gapminder, 2)
tail(gapminder, 10)
```
```
gapminder[1,1]
gapminder[1:5,c(1,3,5)]
gapminder[1,]
```
```
gapminder[gapminder$country=="Australia" & gapminder$year==1952, c("year","lifeExp")]
```
```
subset(gapminder, country=="Australia" & year==1952, select = c(year,lifeExp))
```
#### Challenge 3
Use your new 'subsetting' skills to display the life expectancy and GDPperCapita for people in Paraguay in 2007
# Class 2 - GGPLOT2 -
# Please sign in here:
moved to spreasheet
### Ploting resources
R graph gallery: https://www.r-graph-gallery.com/index.html
ggplot2 cheat sheet: https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
ggplot2 extensions: https://www.ggplot2-exts.org/gallery/
Online book about R data viz: https://r4ds.had.co.nz/data-visualisation.html
```
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point()
```
```
#save plot
ggsave("lifeExpectancy_by_GDPperCap.pdf")
pdf("gdpPercap_vs_time.pdf", width=8, height=8)
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
dev.off()
```
```
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
```
```
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() + theme_classic()
```
```
P <- ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
P <- P + geom_point() + theme_classic()
P #call P to create the plot
```
```
DataOceania <- gapminder[gapminder$continent=="Oceania",]
P <- ggplot(data = gapminder)
P <- P + geom_line(aes(x = year, y = lifeExp, by = country), color="red")
```
```
P <- ggplot(data = gapminder)
P <- P + geom_line(aes(x = year, y = lifeExp, by = country), color = "red")
P <- P + geom_line(data = subset(gapminder, continent == "Oceania"), aes(x = year, y = lifeExp, by =country), color = "black")
P <- P + theme_classic()
P <- P + labs(title = "Oceania on the Rise", x = "Year", y = "Life Expectancy")
P <- P + scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10), labels = seq(0,100,10))
P <- P + scale_x_continuous(breaks = seq(1952,2007,5), labels = seq(1952,2007,5))
p + p = theme_classic()
P
```
```
P <- ggplot(data = gapminder)
P <- P + geom_line(aes(x= year, y = lifeExp, by = country), color = "red")
P <- P + geom_line(data = DataOceania, aes(x= year, y = lifeExp, by = country), color = "black")
P <- P + labs(title = "Oceania on the rise")
P <- P + labs(x = "Year")
P <- P + labs(y = "Life Expectancy")
P <- P + scale_y_continuous(limits = c(0,100), breaks = seq(0, 100, 10), labels = seq(0, 100, 10))
P <- P + scale_x_continuous(breaks = seq(1952, 2007, 5), labels = seq(1952, 2007, 5))
P <- P + theme_classic()
P
```
```
for(iterator in set of values){
do a thing
}
````
```
for(i in 1:10){
print(i)
}
```
```
output_vector <- c()
for(i in 1:5){
for(j in c('a', 'b', 'c', 'd', 'e')){
temp_output <- paste(i, j)
output_vector <- c(output_vector, temp_output)
}
}
output_vector
```
```
plotC<-list()
for (i in c("Africa", "Americas", "Asia", "Europe", "Oceania")) {
data1 <-subset(gapminder, continent==i)
plotC[[i]]<- ggplot(data = data1) + geom_line(aes_string(x='year', y='lifeExp', by='country'), color="red")
}
plotC
```
# Class 3 - Data Wrangling
# Please sign in here:
### A#, full name
A########, Reid Otsuji
moved to spreadsheet
# Useful links:
### Software Carpentries walkthrough
#### http://swcarpentry.github.io/r-novice-gapminder/
### Hadley Wickham's tidy data paper:
#### https://www.jstatsoft.org/article/view/v059i10
# Contact information:
### Justin Shaffer (jpshaffer@ucsd.edu)
#### Location: BRF2 1124
### Subsetting Review
Section 6 - in the lesson link
Atomic vectors
In R, simple vectors containing character strings, numbers, or logical values are called atomic vectors because they can’t be further simplified.
%in% = The way to get R to do what we really want (match each element of the left argument with all of the elements of the right argument) it to use the `%in%` operator.
missing values in R is NA
use funtion `na.omit()`
install.packages("dplyr")
library(dplyr)
section 13 in the Carpentries lesson:
http://swcarpentry.github.io/r-novice-gapminder/13-dplyr/index.html
Challenge 1:
Write a single command (which can span multiple lines and includes pipes) that will produce a dataframe that has the African values for lifeExp, country and year, but not for other Continents. How many rows does your dataframe have and why?
year_country_lifeExp_Africa <- gapminder %>%
filter(continent == "Africa") %>%
select(year, country, lifeExp)
lifeExp_bycountry %>%
arrange(desc(mean_lifeExp)) %>%
head(1)
Create new variables prior to (or even after) summarizing information using `mutate()`.
tidyr:
http://swcarpentry.github.io/r-novice-gapminder/14-tidyr/index.html