﻿ 在如何根据我的函数中的Vector合并数据帧行的重新排序时遇到了困难 - 代码日志

#### 在如何根据我的函数中的Vector合并数据帧行的重新排序时遇到了困难

``````library(tidyverse)
library(ggplot2) for diamonds dataset
``````

``````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){

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){

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)
})
}
``````

``````Fun<-function(df){

Groups<-list("cut","color")

Groups %>%
syms %>%
map_df(~ df %>%
group_by(!!!  .x) %>%
summarise(Count=n()) %>%
set_names(c("Cut","Count")) %>%
slice(match(Order,Cut)) #%>%
#mutate(Cut = as.character(Cut))
#to avoid the warning coercion of factor to character

)

}

Fun(diamonds)
# A tibble: 12 x 2
#   Cut       Count
#   <chr>     <int>
# 1 Good       4906
# 2 Very Good 12082