Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applying function to each group using dplyr and return specified dataframe

Tags:

r

dplyr

I used group_map for the first time and think I do it correctly. This is my code:

library(REAT)

df <- data.frame(value = c(1,1,1, 1,0.5,0.1, 0,0,0,1), group = c(1,1,1, 2,2,2, 3,3,3,3))

haves <- df %>%
    group_by(group) %>%
    group_map(~gini(.x$value, coefnorm = TRUE)) 

The thing is that haves is a list rather than a data frame. What would I have to do to obtain this df

wants <- data.frame(group = c(1,2,3), gini = c(0,0.5625,1))

group gini
1   0.0000
2   0.5625
3   1.0000

Thanks!

like image 747
cs0815 Avatar asked Feb 07 '26 05:02

cs0815


2 Answers

You can use dplyr::summarize:

df %>%
    group_by(group) %>%
    summarize(gini = gini(value, coefnorm = TRUE))

#> # A tibble: 3 x 2
#>   group  gini
#>   <dbl> <dbl>
#> 1     1 0    
#> 2     2 0.562
#> 3     3 1 
like image 112
Allan Cameron Avatar answered Feb 09 '26 01:02

Allan Cameron


According to the documentation, group_map always produces a list. group_modify is an alternative that produces a tibble if the function does, but gini just outputs a vector. So, you could do something like this...

df %>%
  group_by(group) %>%
  group_modify(~tibble(gini = gini(.x$value, coefnorm = TRUE)))

# A tibble: 3 x 2
# Groups:   group [3]
  group  gini
  <dbl> <dbl>
1     1 0    
2     2 0.562
3     3 1  
like image 39
Andrew Gustar Avatar answered Feb 09 '26 01:02

Andrew Gustar