Fighting how to enable vector-based data row reordering in my function

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

?

+4
3

syms . / 1 . syms, map, group_by

Fun<-function(df){

Order<-c("Good","Very Good","Premium","Ideal","Fair","E","F","G","D","H","J","I")

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
# 3 Premium   13791
# 4 Ideal     21551
# 5 Fair       1610
# 6 E          9797
# 7 F          9542
# 8 G         11292
# 9 D          6775
#10 H          8304
#11 J          2808
#12 I          5422
+3

Fun , , Group .

Fun<-function(df){

Order<-c("Good","Very Good","Premium","Ideal","Fair","E","F","G","D","H","J","I")

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

 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(diamonds)
+2

Possible correction to the problem. For simplicity, I created a variable temp_dfand returned it.

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)

      temp <- df%>%
        group_by(!!!syms)%>%
        summarise(Count=n())%>%
        set_names(c("Cut","Count"))
    })

  temp_df <- Groups%>%slice(match(Order, Cut))
  return(temp_df)
}

> x <- Fun(diamonds)
> x
# A tibble: 12 x 2
   Cut       Count
   <chr>     <int>
 1 Good       4906
 2 Very Good 12082
 3 Premium   13791
 4 Ideal     21551
 5 Fair       1610
 6 E          9797
 7 F          9542
 8 G         11292
 9 D          6775
10 H          8304
11 J          2808
12 I          5422
+1
source

Source: https://habr.com/ru/post/1693141/


All Articles