### Davis R Users Group: Teaching R Live Code ### https://tinyurl.com/teachingR ### https://tinyurl.com/teachingRnew # this link goes to raw script, without Dropbox stuff to slow down browser ### FYI: This is a frequently updated R script used for "live-coding" while teaching. Contents may be removed/altered/changed and saved to the Davis R Users Group website: https://d-rug.github.io/ # for today's lesson, we'll be going Halloween themed! # click the following link to download the candy data # https://tinyurl.com/CandySalesData # if we need it, we might also look at some ecological data (boring, I know): https://tinyurl.com/y36xgftg # hello everyone, happy halloween candy <- read.csv(file = "data/cleaned/candy_sales_cleaned.csv") # head() function to look at the top of our data frame head(candy) # View() to look at the whole thing View(candy) my_vector <- c(1, 3, "cat") my_vector # str() for structure str(candy) dim(candy) nrow(candy) ncol(candy) # top or bottom of the dataframe head(candy) tail(candy) # names of your columns and rows names(candy) # column names rownames(candy) # summary() to give a quick summary of the data summary(candy) # tons of ways to subset a dataframe # this will give us the first row and first column of our data candy[1,1] # first element in the 5th column candy[1,5] candy[1,4] # whole 3rd column candy[,3] class(candy[,3]) class(candy) # this method gets us a whole column as a vector # this method gets us the 2nd column as a dataframe candy[2] class(candy[2]) # first 3 entries in the 3rd column (as a vector) candy[1:3,3] # let's pull out one whole row candy[5,] # let's copy what the head() function does candy[1:6,] # let's leave some stuff out candy[,-1] # leave some rows out candy[-c(1:5),] # using column names to pull out data # this one gives us a dataframe back candy["lbs_sold"] # this one gives us a vector candy[,"lbs_sold"] # we need to go deeper: gives us back a vector candy[["lbs_sold"]] # dollar sign subset to pull out a column (another one as a vector) candy$rank # let's get back to logical subsetting candy$rank < 3 candy[candy$rank < 3,] # factor time chocolate <- factor(c("yes", "no", "no", "yes", "yes")) chocolate levels(chocolate) levels(candy$name) # as.character() to convert factors as.character(chocolate) # what happens if we ask for a factor as numeric? as.numeric(chocolate) # factor where levels are numbers years <- factor(c(1998, 1999, 2000)) years as.numeric(years) as.character(years) as.numeric(as.character(years)) read.csv(stringsAsFactors = F)