applying function to each group using dplyr and return specified dataframe

Viewed 50

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!

3 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 

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  

Using data.table

library(data.table)
setDT(df)[, .(gini = gini(value, coefnorm = TRUE)), group] 

For grouped datasets, we can specify .data if in case we don't want to use column names unquoted

library(dplyr)
df %>%
   group_by(group) %>%
   summarize(gini = gini(.data$value, coefnorm = TRUE))
Related