0

I wish to use R and package dplyr to prepare or summarize a column of values using the summarise() (in dplyr) with a user defined function or define it by hand . However, data is not aggregating over the specified group. Please refer to the following passage for more information. The desired result is provided.

library(dplyr)
ver="dplyr version 1.0.5"

#create user defined function intended for summarise()
myfun=function(values){
  values^2}
  

import data

df=data.table(
  grp=c("x","x","y","y","z","z"),
  values=c(2,2,3,3,1,1)
)

apply function "myfun" to values to obtain "func_result" and/or calculate "values" by hand to obtain "by_hand_result" result.


df%>% group_by(grp) %>% summarise(
  func_result=myfun(values),
  by_hand_result=values^2
)

Result, which does not include what is desired or summarized by group ("grp").

`summarise()` has grouped output by 'grp'. You can override using the `.groups` argument.
# A tibble: 6 x 3
# Groups:   grp [3]
grp   func_result by_hand_result
<chr>       <dbl>          <dbl>
1 x               4              4
2 x               4              4
3 y               9              9
4 y               9              9
5 z               1              1
6 z               1              1

Desired Result summarized by "grp".

`summarise()` has grouped output by 'grp'. You can override using the `.groups` argument.
# A tibble: 6 x 3
# Groups:   grp [3]
grp   func_result by_hand_result
<chr>       <dbl>          <dbl>
1 x               8              8
2 y               18             18
3 z               2              2

i have looked at the following posts, however, no success.
Summarize data in R: Summarize data in R

please help summarize the numeric values by group (grp) using a user defined function or calculation by hand with dplyr in R studio. thank you

cn838
  • 69
  • 8

1 Answers1

1

sum the values in the function and by hand result -

library(dplyr)

myfun=function(values) sum(values^2)

df%>% 
  group_by(grp) %>% 
  summarise(func_result=myfun(values),
            by_hand_result= sum(values^2))

#  grp   func_result by_hand_result
#  <chr>       <dbl>          <dbl>
#1 x               8              8
#2 y              18             18
#3 z               2              2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213