Michael
Michael

Reputation: 575

Using list to sum a dataframe in R

Consider the following dataframe in R:

df <- data.frame(Industry = c("Agriculture", "Fishery", "Industry"),
                 Value    = c(10,20,30))

Consider I want to sum the value for "Agriculture" and "Fishery" (10 + 20 = 30). For example, I could do it like this:

df$Value[df$Industry == "Agriculture"] + df$Value[df$Industry == "Fishery"]

However, instead I want to create list with "Agriculture" and "Fishery", and thereafter summing the value. Because in my example I have a big data.frame, and that will make it a lot easier. How to do that?

Upvotes: 1

Views: 55

Answers (3)

January
January

Reputation: 17090

For completeness, a tidyverse approach:

df <- data.frame(Industry = c("Agriculture", "Fishery", "Industry"),
                 Value    = c(10, 20, 30))
df %>% filter(Industry %in% c("Agriculture", "Fishery")) %>%
    summarise(sumValue=sum(Value))

Output:

  sumValue
1       30

Upvotes: 1

Jose
Jose

Reputation: 421

A data.table() approach

library(data.table)

DT <- data.table(Industry = c("Agriculture", "Fishery", "Industry"),
                 Value    = c(10,20,30))

DT[Industry != "Industry"][, sum(Value)]

Output

> [1] 30

Upvotes: 0

Sash Sinha
Sash Sinha

Reputation: 22370

You could use %in%:

df <- data.frame(Industry = c("Agriculture", "Fishery", "Industry"),
                 Value    = c(10, 20, 30))
print(df)
industries_to_sum <- c("Agriculture", "Fishery")
print(sum(df[df$Industry %in% industries_to_sum,]$Value))

Output:

     Industry Value
1 Agriculture    10
2     Fishery    20
3    Industry    30
[1] 30

Upvotes: 2

Related Questions