1

My data looks like this:

loc_ID year_begin  year_end  year  observations
cha89  2002        2004      2002  28
cha89  2002        2004      2003  28
cha89  2002        2004      2004  14  
nin21  2001        2003      2001  90
nin21  2001        2003      2002  12
nin21  2001        2003      2003  99

I want to calculate the sum of observations in the time interval (year_begin to year_end) per loc_ID. So in the above example, my output would be like this:

loc_ID  year_begin  year_end  observations
cha89   2002        2004      70
nin21   2001        2003      201 

How can I do this?

fifigoblin
  • 395
  • 1
  • 8

1 Answers1

2

We can use aggregate in base R

aggregate(observations ~ loc_ID + year_begin + year_end, df1, FUN = sum)

-output

# loc_ID year_begin year_end observations
#1  nin21       2001     2003          201
#2  cha89       2002     2004           70

data

df1 <- structure(list(loc_ID = c("cha89", "cha89", "cha89", "nin21", 
"nin21", "nin21"), year_begin = c(2002L, 2002L, 2002L, 2001L, 
2001L, 2001L), year_end = c(2004L, 2004L, 2004L, 2003L, 2003L, 
2003L), year = c(2002L, 2003L, 2004L, 2001L, 2002L, 2003L),
observations = c(28L, 
28L, 14L, 90L, 12L, 99L)), class = "data.frame", row.names = c(NA, 
-6L))
akrun
  • 874,273
  • 37
  • 540
  • 662