Hey all,
Thanks in advance for the help. I'm trying to order data by levels of one factor, and then by the sum of numeric data associated with another factor. Secondly, I'm trying to apply a group number. I want to do this without manual input, such that a script can be saved for different, future data. To clarify, I've created an example dataset:
library(dplyr)
Year <- c("2019", "2019", "2019", "2019", "2019", "2019", "2020",
"2020", "2020", "2019", "2019", "2019", "2020", "2020", "2020", "2019", "2019", "2019")
Model <- factor(c("Mustang", "Mustang", "Mustang", "F150", "F150", "F150", "F150", "F150",
"F150", "Cruise", "Cruise", "Cruise", "Camaro", "Camaro", "Camaro", "Cruise",
"Cruise", "Cruise"))
Make <- factor(c("Ford", "Ford", "Ford", "Ford", "Ford", "Ford", "Ford", "Ford", "Ford",
"Chevy", "Chevy", "Chevy", "Chevy", "Chevy", "Chevy", "Toyota", "Toyota", "Toyota"),
levels = c("Ford", "Chevy", "Toyota"))
Color <- factor(c("Red", "Blue", "Green", "Red", "Blue", "Green", "Red", "Blue", "Green",
"Red", "Blue", "Green", "Red", "Blue", "Green", "Red", "Blue", "Green"),
levels = c("Red", "Blue", "Green"))
Purchases <- c(20, 35, 10, 32, 49, 45, 37, 53, 25, 55, 24, 13, 32, 45, 60, 12, 20, 13)
Data <- data.frame(Year, Model, Make, Color, Purchases)
Data.order <- Data %>%
group_by(Make,Model) %>%
summarize(Sum=sum(Purchases)) %>%
arrange(Make,-Sum)
The 1st level of arrangement must be the Make, and then I want to re-order the Models according to the sum of purchases of the 3 different colors. Year is not relevant for ordering. I've attached a screenshot of the end goal.
Progress/issue: I've tried to sum and order the data into another data frame, and then reorder the levels of the primary data frame according to the new levels taken from the sum data frame, before arranging accordingly. However, this approach isn't working because it turns out I have repeated levels in the Models, due to different Makes sharing the same Model name, "Cruise". Perhaps I can apply a consecutive group number sequence within the new Order data frame, and then cross-match the group numbers back to primary data frame to arrange by Type, Group number?
Thanks a lot everyone!!