Silent_bliss
Silent_bliss

Reputation: 307

Error is using absolute difference in dplyr package?

I have a data frame that looks like this.

df <- data.frame(Year = c(2016,2017,2016,2017),
                 Month = c(1,2,1,2),
                 valueA = c(40,29,25,22),
                 valueB = c(40,36,31,30))

I wish to calculate the difference by grouping the columns by year and month.

abs(aggregate(.~ Year + Month, df, diff))
#  Year Month valueA valueB
#1 2016     1     15      9
#2 2017     2      7      6

The same thing if I am trying in dplyr package, it is not working.

 library(dplyr)
    df %>% group_by(Year, Month) %>% summarise_all(abs(diff))
    #Error in abs(diff) : non-numeric argument to mathematical function

On the other hand, if I remove abs function, it works.

df %>% group_by(Year, Month) %>% summarise_all((diff))
 

    #Year Month valueA valueB
     # <dbl> <dbl>  <dbl>  <dbl>
    #1  2016     1    -15     -9
    #2  2017     2     -7     -6

Any idea as to why the error is coming and the how can desired output be achieved from dplyr package?

Upvotes: 0

Views: 60

Answers (2)

codez0mb1e
codez0mb1e

Reputation: 896

You can try to calculate absolute values after summarise_all:

df %>% 
  group_by(Year, Month) %>%
  summarise_all(diff) %>% 
  mutate_all(abs)

Upvotes: 1

stefan
stefan

Reputation: 124393

If you want to apply one function you can simply pass summarise the function name. However, if you want to apply a composite function you have to be a bit more explicit. Try this:

df <- data.frame(Year = c(2016,2017,2016,2017),
                 Month = c(1,2,1,2),
                 valueA = c(40,29,25,22),
                 valueB = c(40,36,31,30))

library(dplyr)
df %>% group_by(Year, Month) %>% summarise_all(~ abs(diff(.x)))
#> # A tibble: 2 x 4
#> # Groups:   Year [2]
#>    Year Month valueA valueB
#>   <dbl> <dbl>  <dbl>  <dbl>
#> 1  2016     1     15      9
#> 2  2017     2      7      6

Upvotes: 1

Related Questions