A short description of the post.
First, I loaded in the packages and files I needed.I am using the poultry data set for this project.
I first used the functions select, filter, and arrange. I saved sumarize for when I ran the function group_by to highlight the function’s use. After those, I used rename, case_when, across, pivot_longer and wider, purrr, and lapply.
#Functions
#select, filter, arrange
poultry2 <- poultry %>%
select(Product, Year, Price_Dollar) %>%
filter(Product == "Whole") %>%
arrange(Product, Price_Dollar, Year)
head(poultry2)
Product Year Price_Dollar
1 Whole 2004 1.97500
2 Whole 2004 1.97500
3 Whole 2004 2.09000
4 Whole 2004 2.12000
5 Whole 2004 2.14500
6 Whole 2004 2.16375
head(poultry)
Product Year Month Price_Dollar
1 Whole 2013 January 2.385
2 Whole 2013 February 2.385
3 Whole 2013 March 2.385
4 Whole 2013 April 2.385
5 Whole 2013 May 2.385
6 Whole 2013 June 2.385
#Optional Functions
#group_by & summarize
poultry_grouped <- poultry2 %>%
group_by(Year)
summarise(poultry_grouped, Year = mean(Year))
# A tibble: 10 × 1
Year
<dbl>
1 2004
2 2005
3 2006
4 2007
5 2008
6 2009
7 2010
8 2011
9 2012
10 2013
#rename
colnames(poultry2)
[1] "Product" "Year" "Price_Dollar"
[1] 2.305333
poultry4 <- poultry2 %>%
mutate(Above.Below.Avg = case_when(Price_Dollar >= 2.305333 ~"Above Mean", TRUE ~ "Below Mean"))
#across
poultry5 <- poultry2 %>%
group_by(Year) %>%
summarise(across(starts_with("Price_Dollar"), list(mean = mean, sd = sd)))
#Pivoting
poultry_wider <- poultry %>%
pivot_wider(names_from = "Product",values_from = "Price_Dollar")
poultry_longer <- poultry_wider %>%
pivot_longer(Whole:Thighs, names_to = "Product", values_to = "Price_Dollar")
#purrr
library(stringr)
avg_fun <- function(x)
purrr::map_dbl(poultry3, n_distinct)
#lapply
poultry_upper <- lapply(poultry2$Product, toupper)
str(poultry_upper)
List of 120
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
$ : chr "WHOLE"
[list output truncated]
I decided to use ggplot to edify my knowledge of it. Here is the poultry’s Product variable’s price plotted by the month and year.
#plotting
ggplot(data = poultry, mapping = aes(x=Month, y = Price_Dollar, color = Product, group = Product))+
geom_point()+
geom_line() +
facet_grid(~Year)
Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Manning (2021, Aug. 20). DACSS 601 August 2021: Homework3: Functions by MM. Retrieved from https://mrolfe.github.io/DACSS601August2021/posts/2021-08-20-homework3-functions-by-mm/
BibTeX citation
@misc{manning2021homework3:, author = {Manning, Michelle}, title = {DACSS 601 August 2021: Homework3: Functions by MM}, url = {https://mrolfe.github.io/DACSS601August2021/posts/2021-08-20-homework3-functions-by-mm/}, year = {2021} }