library(tidyverse)
library(ggplot2) for diamonds dataset
I have problems working my function. What I'm trying to do using the ggplot2 diamond dataset for this example is dplyr :: group_by "cut" and "color", then dplyr :: summary to get the counts. I used rlang and purrr to list both counter summaries in a list, then renamed one of the columns and linked them to dplyr :: map_df. Finally, I want to reorder the Cut column based on another Order vector. The function works until I try to enable row reordering ...
This may not make sense for this data, but it is just an example, and it makes sense for my real data.
Anyway, the code below works ...
Groups<-list("cut","color")
Groups<-Groups%>%
map_df(function(group){
syms<-syms(group)
diamonds%>%
group_by(!!!syms)%>%
summarise(Count=n())%>%
set_names(c("Cut","Count"))
})
"", .
Order<-c("Good","Very Good","Premium","Ideal","Fair","E","F","G","D","H","J","I")
Groups%>%slice(match(Order, Cut))
, . , , , . , - ...
Fun<-function(df){
Order<-c("Good","Very Good","Premium","Ideal","Fair","E","F","G","D","H","J","I")
Groups<-list("cut","color")
Groups<-Groups%>%
map_df(function(group){
syms<-syms(group)
df%>%
group_by(!!!syms)%>%
summarise(Count=n())%>%
set_names(c("Cut","Count"))%>%
slice(match(Order,Cut))
return(df)
})
}
...
Fun<-function(df){
Order<-c("Good","Very Good","Premium","Ideal","Fair","E","F","G","D","H","J","I")
Groups<-list("cut","color")
Groups<-Groups%>%
map_df(function(group){
syms<-syms(group)
df%>%
group_by(!!!syms)%>%
summarise(Count=n())%>%
set_names(c("Cut","Count"))
df<-df%>%slice(match(Order,Cut))
return(df)
})
}
?