#**************************************************# #R tutorial for 2019 NANP Nutrition Models Workshop# #**************************************************# #Tim Hackmann #University of California, Davis ############################### #Perform some basic operations# ############################### #Perform simple addition #Run by highlighting line below, then clicking "Edit" -> "Run line or selection" a=2+2 #Output the result print(a) ############################################################### #Prepare R for bigger and better things by installing packages# ############################################################### #Download and install the package install.packages("ggplot2") #If you don't have internet, you can install the packages from the USB drive #Uncomment before running #Switch running directory to location of packages #Replace "C:/My Directory/" with the actual directory name #setwd("C:/My Directory/") #Install packages #pkgFilenames <- read.csv("pkgFilenames.csv", stringsAsFactors = FALSE)[, 1] #install.packages(pkgFilenames, repos = NULL, type = "win.binary") #If you are running a Mac, run this instead #pkgFilenames <- read.csv("pkgFilenames_mac.csv", stringsAsFactors = FALSE)[, 1] #install.packages(pkgFilenames, repos = NULL, type = "mac.binary") #Load the package library(ggplot2) ############## #Read in data# ############## #Switch working directory to location of data #Replace "C:/My Directory/" with the actual directory name setwd("C:/My Directory/") #Read in data mtcars_df <- read.csv(file="R tutorial data file.csv", header=TRUE, sep=",") #################### #View and plot data# #################### #View all data mtcars_df #View weight (wt) only mtcars_df$wt #View mpg (mpg) only mtcars_df$mpg #Plot wt vs. mpg with ggplot ggplot(data = mtcars_df, mapping = aes(x = wt, y = mpg)) + geom_point() ################# #Manipulate data# ################# #Calculate weight on moon (wt_moon) wt_multiplier = 0.165188131 #Ratio of weight on moon to weight on earth mtcars_df$wt_moon = mtcars_df$wt*wt_multiplier #View data with wt_moon added mtcars_df #Plot wt_moon vs. mpg with ggplot ggplot(data = mtcars_df, mapping = aes(x = wt_moon, y = mpg)) + geom_point() #################### #Export data as csv# #################### write.csv(mtcars_df, file = "R tutorial data file modified.csv") ##################################### #Download packages for the exercises# ##################################### #If you installed packages from the USB drive, you have already done this and don't need to run the code below install.packages("boot") install.packages("caret") install.packages("deSolve") install.packages("epiR") install.packages("FME") install.packages("ggeffects") install.packages("ggplot2") install.packages("gridExtra") install.packages("lme4") install.packages("MuMIn") install.packages("parallel") install.packages("png") install.packages("readr") install.packages("reshape2") install.packages("rlang") install.packages("sjlabelled") install.packages("sjmisc") install.packages("sjPlot") install.packages("sjstats")