Skip to content
Prev 385122 / 398503 Next

Arrange data

On 2020-08-03 21:11 +1000, Jim Lemon wrote:
Hi Md,

you can also define the period in a new 
column, and use aggregate like this:

	Md <- structure(list(
	Year = c(2000L, 2000L, 2000L, 
	2000L, 2001L, 2018L, 2018L), 
	Month = c(1L, 2L, 3L, 12L, 1L,
	11L, 12L), 
	Value = c(25L, 28L, 22L, 26L,
	27L, 30L, 29L)), 
	class = "data.frame", 
	row.names = c(NA, -7L))
	
	Md[Md$Month %in%
	        1:6,"Period"] <- "first six months of the year"
	Md[Md$Month %in% 7:12,"Period"] <- "last six months of the year"
	
	aggregate(
	  formula=Value~Year+Period,
	  data=Md,
	  FUN=mean)

Rasmus