Error in eval(expr, envir, enclos): object 'a' not found
Workshop: Dealing with Data in R
steffilazerte
@steffilazerte@fosstodon.org
@steffilazerte
steffilazerte.ca
Compiled: 2024-02-21
Save previous script
Consider taking notes during this section
Artwork by @allison_horst
Artwork by @allison_horst
library(tidyverse)
# Load Data
size <- read_csv("./data/grain_size2.csv")
# First modification
size <- mutate(size,
total_sand = coarse_sand + medium_sand + fine_sand,
total_silt = coarse_silt + medium_silt + fine_silt)
# Second modification
size <- size |>
group_by(plot) |>
summarize(n = n(),
total_sand = sum(total_sand),
mean_sand = mean(total_sand),
sd_sand = sd(total_sand),
se_sand = sd_sand / sqrt(n))
Especially important if loading and modifying data
Can’t run 1st modification
after 2nd modification
library(tidyverse)
size <- read_csv("./data/grain_size2.csv") |>
mutate(total_sand = coarse_sand + medium_sand + fine_sand,
total_silt = coarse_silt + medium_silt + fine_silt) |>
group_by(plot) |>
summarize(n = n(),
total_sand = sum(total_sand),
mean_sand = mean(totall_sand),
sd_sand = sd(total_sand),
se_sand = sd_sand / sqrt(n))
Error in `summarize()`:
ℹ In argument: `mean_sand = mean(totall_sand)`.
ℹ In group 1: `plot = "CSP01"`.
Caused by error:
! object 'totall_sand' not found
No error
No error
size <- read_csv("./data/grain_size2.csv") |>
mutate(total_sand = coarse_sand + medium_sand + fine_sand,
total_silt = coarse_silt + medium_silt + fine_silt) |>
group_by(plot) |>
summarize(n = n(),
total_sand = sum(total_sand),
mean_sand = mean(totall_sand),
sd_sand = sd(total_sand),
se_sand = sd_sand / sqrt(n))
Error in `summarize()`:
ℹ In argument: `mean_sand = mean(totall_sand)`.
ℹ In group 1: `plot = "CSP01"`.
Caused by error:
! object 'totall_sand' not found
Ah ha!
Error: Problem with `summarise()` column `mean_sand`.
ℹ `mean_sand = mean(totall_sand)`.
x object 'totall_sand' not found
ℹ The error occurred in group 1: plot = "CSP01".
Error: Problem with 'summarise()' column 'mean_sand'`
Okay, we know the problem is in the summarize()
part and then mean_sand
part of that
ℹ 'mean_sand = mean(totall_sand)'
x object 'totall_sand' not found
Looks like this is the line with the problem.
And the problem is object 'totall_sand' not found
.
Ooops! Typo!
ℹ The error occurred in group 1: plot = "CSP01".
Lastly, it’s telling us that the problem was when working with this group of data.
(This can be useful when troubleshooting, because you can filter()
your data and take a look)
Artwork by @allison_horst
Just sometimes unhelpful!
Take a look yourself
Many packages come with vignettes (tutorials)
Vignettes in package ‘ggplot2’:
ggplot2-specs Aesthetic specifications (source, html)
extending-ggplot2 Extending ggplot2 (source, html)
profiling Profiling Performance (source, html)
ggplot2
(next level)
Copy and paste the examples into your console
tidyverse
? R base? A mix?)mtcars
and iris
are commonly used data sets built into R basemsleep
and diamonds
are commonly used data sets built into ggplot2
library()
)vore
?ggplot(data = m, aes(x = vore, y = awake, fill = `Body Size`)) +
theme_bw() +
theme(axis.title.x = element_blank()) +
geom_boxplot() +
scale_fill_viridis_d() +
labs(y = "Awake time (hrs)",
title = "Awake time by Diet")
Error in eval(expr, envir, enclos): object 'm' not found
m
isvore
?library(ggplot2)
m <- msleep |>
mutate(`Body Size` = if_else(bodywt > median(bodywt),
"Large", "Small"))
ggplot(m, aes(x = vore, y = awake, fill = `Body Size`)) +
theme_bw() +
theme(axis.title.x = element_blank()) +
geom_boxplot() +
scale_fill_viridis_d() +
labs(y = "Awake time (hrs)",
title = "Awake time by Diet")
vore
?“All statistical analyses were performed with R statistical software (v4.3.2, R Core Team 2023).”
R Core Team (2023). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.
To cite R in publications use:
R Core Team (2023). _R: A Language and Environment for Statistical
Computing_. R Foundation for Statistical Computing, Vienna, Austria.
<https://www.R-project.org/>.
“All statistical analyses were performed with R statistical software (v4.0.3, R Core Team 2020). We performed Type III ANOVAs using the ‘car’ package for R (v3.0.10, Fox and Weisberg 2019).”
John Fox and Sanford Weisberg (2019). An R Companion to Applied Regression, Third Edition. Thousand Oaks CA: Sage.
To cite the car package in publications use:
Fox J, Weisberg S (2019). _An R Companion to Applied Regression_,
Third edition. Sage, Thousand Oaks CA.
<https://socialsciences.mcmaster.ca/jfox/Books/Companion/>.
See more about citing packages in my rOpenSci blog post: How to Cite R and R packages
Artwork by @allison_horst
(Feedback!)