Cheat Sheet For Programming in R

By Wendy Wong

1.Filter for one variable in R

library(gapminder) library(dplyr)

Filter the gapminder dataset for the year 1957

gapminder %>% filter(year==1957)

2. Assign a value to the variable my_apples in R

my_apples <- 5

3. Create a vector in R

days_vector <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”)

4. Sort a list in R

x <- c(4,1,2,3) x <- sort(x) x

5. Create a line chart in R

library(gapminder) library(dplyr) library(ggplot2)

by_year <- gapminder %>% group_by(year) %>% summarize(medianGdpPercap = median(gdpPercap))

Create a line plot showing change in medianGdpPercap over time

ggplot(by_year, aes(x = year, y = medianGdpPercap)) + geom_line() + expand_limits(y = 0)

6. Adding a title to your graph in R

library(gapminder) library(dplyr) library(ggplot2)

gapminder_1952 <- gapminder %>% filter(year == 1952)

Add the title to this graph: “Comparing GDP per capita across continents”

ggplot(gapminder_1952, aes(x= continent, y = gdpPercap)) + geom_boxplot() + scale_y_log10() + ggtitle(“Comparing GDP per capital across continents”)

7.Read a data frame as a csv file in R

Set working directory

setwd(“~/Desktop/DSP”)

Load package dplyr for data manipulation

library(dplyr)

Read a data frame as a csv file

df <- read.csv(“Complaint_Problems.csv”)

8. Build a Data Frame with three key: value pairs in R

Pre-defined vectors

name <- c(“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”) type <- c(“Terrestrial planet”, “Terrestrial planet”, “Terrestrial planet”, “Terrestrial planet”, “Gas giant”,“Gas giant”,“Gas giant”, “Gas giant”) diameter <- c(0.382,0.949,1,0.532, 11.209,9.449, 4.007, 3.883) rotation <- c(58.64, -243.02,1,1.03,0.41, 0.43, -0.72,0.67) rings <- c(FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE)

Create a data frame from the vectors in R

planets_df <- data.frame(name, type, diameter, rotation, rings)

Subset a data frame in R

Select planets with a diameter < 1

subset(planets_df, diameter < 1)

9. Create a scatterplot in R

Load libraries

library(gapminder) library(dplyr) library(ggplot2)

Summarize the median GDP and median life expectancy per continent in 2007

by_continent_2007 <- gapminder %>% filter(year == 2007) %>% group_by(continent) %>% summarize(medianGdpPercap = median(gdpPercap), medianLifeExp = median(lifeExp))

Use a scatterplot to compare the median GDP and median life expectancy

ggplot(by_continent_2007, aes(x = medianGdpPercap, y = medianLifeExp, color = continent)) + geom_point()

10. Ensuring a line plot commences on the y-axis where y = 0 in R

library(gapminder) library(dplyr) library(ggplot2)

by_year <- gapminder %>% # assign a new variable name group_by(year) %>% summarize(medianLifeExp = median(lifeExp), maxGdpPercap = max(gdpPercap))

by_year # save the dataset

ggplot(by_year, aes(x=year, y=medianLifeExp)) + geom_point()+ expand_limits(y=0)