Search code examples
rlistsum

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?


Solution

  • 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