JeffWithpetersen
JeffWithpetersen

Reputation: 147

SUMIF rows equivalent in R

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.

Upvotes: 1

Views: 205

Answers (1)

PaulS
PaulS

Reputation: 25323

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

Upvotes: 1

Related Questions