Dataframes

Ranae Dietzel & Andee Kaplan

Dataframes

DIY Dataframe

Good to know for making simple examples

block = c("A", "B", "C", "A", "B", "C")
rep = c(1, 2, 1, 2, 1, 2)
mass = c(52, 48, 33, 32, 44, 49)
df = data.frame(block, rep, mass)

What have you done?

plot(df$mass ~ df$rep)
df$rep <- as.factor(df$rep)
plot(df$mass ~ df$rep)

If you get an error message, check your class!

Importing data

Check your working directory. getwd()
Change your working directory. setwd()
Import your file
df <- read.table("file.txt", header = TRUE)
df <- readr::read_csv("file.csv")

For Excel files, look in to the readxl package

Subsetting

%in% (?‘%in%’) (use quotes when looking up weird stuff)

| means “or”

otherwise & means “and”

[] vs [[]]

Let’s import and subset

Your turn

  1. Use the NASS data to determine the relationship between time and change in bu/acre of corn
  2. Examine the same relationship, but split into 1940-1990 and 1991-2015 and compare the slopes of the linear models
  3. Compare “acres harvested” of small grains (oats, barley, wheat, and rye) to “acres harvested” of corn and soybean over the last 100 years.