Search code examples
rgroup-bysumrowslice

rowSums After Slicing in R


I have dataframe like this. First I arrange and then slice by DPP. Like this: But after arrange and slice. I cant have rowsums

card_201406 <- data.frame(ID = c(123, 234, 344, 456, 678, 124, 567, 256, 345), 
                          Block_Code = c("D", "U","Z", "G","T","R","A","U", "B"),
                          DPP = c(1,2,2,3,3,3,4,5,1),
                          a = 1:9, a_1 = 1:9, a_2 = 1:9, a_3 = 1:9, a_4 = 1:9)

card_201406 <- card_201406 %>% arrange(DPP) %>% group_by(DPP) %>% slice(1)
card_201406 <- card_201406 %>% 
  mutate(SUM_a = rowSums(do.call(cbind, select(.,starts_with("a_")))))

RESULT:

Adding missing grouping variables: `DPP`
Error in `mutate()`:
! Problem while computing `SUM_a = rowSums(do.call(cbind, select(.,
  starts_with("a_"))))`.
x `SUM_a` must be size 1, not 5.
i The error occurred in group 1: DPP = 1.
Run `rlang::last_error()` to see where the error occurred.

I just want sum perrows

Thanks for helping


Solution

  • Haven't checked what's wrong but you could simplify using across:

    library(dplyr, warn=FALSE)
    
    card_201406 %>% 
      arrange(DPP) %>% 
      group_by(DPP) %>% 
      slice(1) %>%
      mutate(SUM_a = rowSums(across(starts_with("a_"))))
    #> # A tibble: 5 × 9
    #> # Groups:   DPP [5]
    #>      ID Block_Code   DPP     a   a_1   a_2   a_3   a_4 SUM_a
    #>   <dbl> <chr>      <dbl> <int> <int> <int> <int> <int> <dbl>
    #> 1   123 D              1     1     1     1     1     1     4
    #> 2   234 U              2     2     2     2     2     2     8
    #> 3   456 G              3     4     4     4     4     4    16
    #> 4   567 A              4     7     7     7     7     7    28
    #> 5   256 U              5     8     8     8     8     8    32