Hallie Sheikh
Hallie Sheikh

Reputation: 431

Conversion of monthly data to yearly data in a dataframe in r

I have a dataframe showing monthly mgpp from 2000-2010:

dataframe1 
     Year Month       mgpp
  1: 2000     1 0.01986404
  2: 2000     2 0.011178429
  3: 2000     3 0.02662008
  4: 2000     4 0.05034293
  5: 2000     5 0.23491388
 ---                      
128: 2010     8 0.13234501
129: 2010     9 0.10432369
130: 2010    10 0.04329537
131: 2010    11 0.04343289
132: 2010    12 0.09494946

I am trying to convert this dataframe1 into a raster that will show the variable mgpp. However I want to format the dataframe first which will show only the yearly mgpp. The expected outcome is shown below :

dataframe1
     Year       mgpp
  1: 2000      0.01986704
  2: 2001      0.01578429
  3: 2002      0.02662328
  4: 2003      0.05089593
  5: 2004      0.07491388

6: 2005        0.11229201
7: 2006        0.10318569
8: 2007        0.07129537
9: 2008        0.04373689
10: 2009       0.02885386
11: 2010       0.74848348

I want to aggregate the months by mean. For instance, 2000 value shows one value that is the mean from Jan-Dec for the 2000 year.How can I achieve this? Help would be appreciated

Upvotes: 0

Views: 1067

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24770

Here a data.table approach.

library(data.table)
setDT(dataframe1)[,.(Yearly.mgpp = mean(mgpp)),by=Year]
   Year Yearly.mgpp
1: 2000  0.06858387
2: 2010  0.08366928

Or if you prefer dplyr.

library(dplyr)
dataframe1 %>% 
  group_by(Year) %>% 
  summarise(Yearly.mgpp = mean(mgpp))

# A tibble: 2 x 2
   Year Yearly.mgpp
  <dbl>       <dbl>
1  2000      0.0686
2  2010      0.0837

Or base R.

result <- sapply(split(dataframe1$mgpp,dataframe1$Year),mean)
data.frame(Year = as.numeric(names(result)),Yearly.mgpp = result)
     Year Yearly.mgpp
2000 2000  0.06858387
2010 2010  0.08366928

Sample Data

dataframe1 <- structure(list(Year = c(2000, 2000, 2000, 2000, 2000, 2010, 2010, 
2010, 2010, 2010), Month = c(1, 2, 3, 4, 5, 8, 9, 10, 11, 12), 
    mgpp = c(0.01986404, 0.011178429, 0.02662008, 0.05034293, 
    0.23491388, 0.13234501, 0.10432369, 0.04329537, 0.04343289, 
    0.09494946)), class = "data.frame", row.names = c(NA, -10L
))

Upvotes: 1

Related Questions