0

I'm trying to average several time period of a particular variable column in a unit-time dataframe into a new datafame that is just unit and average of each time periods. I'm not quite sure how to do this, can anyone help?

Base data:

A <- structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "USA", class = "factor"), 
year = c(2000, 2001, 2002, 2003, 2004, 2005), var1 = c(2, 
5, 4, 6, 7, 8)), class = "data.frame", row.names = c(NA, -6L))

Desired output

B <- structure(list(country1 = structure(1L, .Label = "USA", class = "factor"), 
var1.2000.2002 = 3.667, var1.2003.2005 = 7), class = "data.frame", row.names = c(NA, -1L))  
ThaiTeasy
  • 11
  • 2
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not helpful because we can't copy/paste the data into R for testing. How are you deciding how to make groups for your Year variable? – MrFlick Jun 25 '20 at 23:35
  • `aggregate(var1~country, A, mean)` – Ronak Shah Jun 26 '20 at 00:05
  • Hi, what if I want to average them with different time periods? Like average var1 based on specific country and time period (e.g. 2000-2002) and another time period (2003-2005)? – ThaiTeasy Jun 26 '20 at 00:15

0 Answers0