Home > Mobile >  SUMIF rows equivalent in R
SUMIF rows equivalent in R

Time:03-06

I am looking to produce summary rows in R so that a table like this:

Region Area Numerator Denominator
North AreaA 1 10
North AreaB 1 10
South AreaC 1 10
South AreaD 1 10

Becomes this:

Region Numerator Denominator
North 2 20
South 2 20

Very simple I'm sure for someone who understands the equivalent of SUMIF (Excel) for R. I have looked at other questions but am too new to R to figure this out. Any help very much appreciated.

CodePudding user response:

A base R solution:

df <- data.frame(
  stringsAsFactors = FALSE,
  Region = c("North", "North", "South", "South"),
  Area = c("AreaA", "AreaB", "AreaC", "AreaD"),
  Numerator = c(1L, 1L, 1L, 1L),
  Denominator = c(10L, 10L, 10L, 10L)
)

aggregate(cbind(Numerator, Denominator) ~ Region, data = df, FUN = sum)

#>   Region Numerator Denominator
#> 1  North         2          20
#> 2  South         2          20

Another possible solution, based on dplyr:

library(dplyr)

df %>% 
  group_by(Region) %>% 
  summarise(across(-Area, sum))

#> # A tibble: 2 × 3
#>   Region Numerator Denominator
#>   <chr>      <int>       <int>
#> 1 North          2          20
#> 2 South          2          20
  •  Tags:  
  • r
  • Related