# export with write.csv()
write.csv(ToothGrowth,
file = "exports/tooth_growth.csv",
row.names = FALSE, # remove rowname column
na = "") # empty cell instead of "NA"
UQRUG 17
meeting
2021-06-21: UQRUG 17
Participants
- Stéphane Guillou (Library): just here to share help!
- Catherine Kim (Library/Biology): playing around spatial and open datasets from this World’s Oceans Day R-blogger post
- … and 8 more UQRUGers!
Tips for newcomers
- Projects for everything! Using R Projects allows you to keep separate projects… separate! And find files easily. Start organised to stay organised.
- R 4 Data Science book: https://r4ds.had.co.nz/
Code snippets
Export to CSV
Missing data handling in dplyr::filter()
# insert missing data:
27,3] <- NA
ToothGrowth[library(dplyr)
# dplyr filtering:
<- filter(ToothGrowth, dose > 0.5)
filtered_dplyr # dplyr filter gets rid of rows returning NA
# base indexing:
<- ToothGrowth[ToothGrowth$dose > 0.5, ]
filtered_base # base subsetting with [] keeps rows returning NA
# check documentation:
::filter ?dplyr
“[In
dplyr::filter
], note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with[
.”
New alt
argument in ggplot2
Great news for accessibility in the new ggplot2 version 3.3.4: https://cloud.r-project.org/web/packages/ggplot2/news/news.html
For example:
library(ggplot2)
ggplot(ToothGrowth, aes(x = supp, y = len)) +
geom_boxplot() +
labs(alt = "Boxplot of tooth growth rate with different supplements.\n
The length is generally higher with the OJ supplement.")
This will allow tools that create documents that include ggplot2 visualisations to make use of the alternative text, for screen readers for example.
Know that R Markdown has the chunk option fig.alt
for that purpose as well.