getwd() setwd("/Users/meridithlavelle/Desktop/Intro to R - Fall 2022") #make sure to set this #to reflect the filepath on your own device 1 + a a <- 1 1 + a a <- 4 a.1 <- 1 a.4 <- 4 #installing and loading packages install.packages("foreign") library(foreign) install.packages("readxl") install.packages("readr") install.packages("rio") library(readxl) library(readr) library(rio) library(carData) help(package = "carData") ?WVS ?library x <- 1 y <- 2 x * y x/y log(x) exp(x) class(x) xvec <- c(1, 2, 3, 4, 5) xvec xvec <- c(1:5) xvec2 <- seq(from = 1, to = 5, by = 1) xvec2 yvec <- rep(1,5) yvec ?rep zvec_add <- xvec + yvec zvec_add mat1 <- matrix(data = c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE) mat1 mat2 <- matrix(data = c(1, 2, 3, 4, 5, 6), ncol = 3) mat2 # lines 51-52 not originally included on the pdf - this example was used to demonstrate #that we can change the nrow specificatin to ncol mat3 <- matrix(data = seq(from = 6, to = 3.5, by = -0.5), nrow = 2, byrow = TRUE) mat3 mat1 %*% mat2 rm(matx) #deleting a single object #Data Frames grade <- c("A", "D", "A-", "B+", "A", "A") days_absent <- c(1, 9, 2, 3, 0, 1) name <- c("Student 1", "Student 2", "Student 3", "Student 4", "Student 5", "Student 6") mydata <- data.frame(name, grade, days_absent) mydata vec1 <- c(2, 4, 6, 8) vec1[3] mydata[1,] mydata[,3] mydata[2,2] mydata[2:4, "days_absent"] mydata$grade draws <- rnorm(1000, mean = 5, sd = 10) summary(draws) plot(density(draws), main = "Title of plot", xlab = "X-axis", ylab = "Y-Axis") draws <- rnorm(1000, mean = 5, sd = 10) hist.plot <- hist(draws, main = "Histogram", xlab = "X-axis", ylab = "Y-axis") hist.plot #loading in data from your device hr_conflict <- read.csv("hr_conflict.csv") summary(hr_conflict) sd(hr_conflict$population) sd(hr_conflict$population, na.rm = TRUE) quantile(hr_conflict$population, probs = c(0.05, 0.95), na.rm = TRUE) #random draws/distrubutions set.seed(321) dist1 <- rnorm(n = 1000, mean = 0, sd = 1) set.seed(321) dist2 <- rnorm(n = 1000, mean = 0, sd = 2) plot(density(dist1)) lines(density(dist2), col = "red") set.seed(321) x <- 1:50 y <- rnorm(n = 50, mean = 0, sd = 2) plot(x, y, pch = 1, col = "blue") set.seed(123) x1 <- rnorm(100) boxplot(x, col = "grey", main = "Box plot") boxplot(x, col = "grey", main = "Box plot", xlab = "X", ylab = "Y") #Exporting plots set.seed(321) dist1 <- rnorm(n = 1000, mean = 0, sd = 1) set.seed(321) dist2 <- rnorm(n = 1000, mean = 0, sd = 2) pdf("density_plot.pdf", width = 5, height = 5) plot(density(dist1)) lines(density(dist2), col = "red") dev.off() #ggplot library(ggplot2) library(reshape) library(gapminder) gapminder <- gapminder summary(gapminder) ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point() ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point() + scale_x_log10() ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point() ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + scale_x_log10() + facet_wrap(~continent) ggplot(gapminder, aes(x = continent, y = lifeExp)) + geom_col() ggplot(gapminder, aes(x = continent, y = lifeExp, color = continent)) + geom_col() set.seed(321) dist1 <- rnorm(n = 1000, mean = 0, sd = 1) set.seed(321) dist2 <- rnorm(n = 1000, mean = 0, sd = 2) dist.data <- data.frame(dist1, dist2) ggplot(dist.data, aes(dist1)) + geom_density() ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + scale_x_log10() + facet_wrap(~continent) + xlab("GDP per capita") + ylab("Life Expectancy") + ggtitle("GDP's effect on life expectancy by continent") final.plot <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + scale_x_log10() + facet_wrap(~continent) + xlab("GDP per capita") + ylab("Life Expectancy") + ggtitle("GDP's Effect on Life Expectancy by Continent") ggsave("final.plot.pdf")