[R] Coversion from yearly to weekly data

Rui Barradas ruipbarradas at sapo.pt
Fri Mar 8 16:42:14 CET 2013


Hello,

Something like this?


sp <- split(test, test$Data)
res <- do.call(rbind, lapply(sp, function(x){
	Week <- (seq_len(nrow(x)) %/% 7) + 1
	aggregate(Value ~ Data + Week, data = x, FUN = mean)}))
rownames(res) <- seq_len(nrow(res))
res


Hope this helps,

Rui Barradas

Em 08-03-2013 15:16, Nico Met escreveu:
> Dear all, I have a big data matrix and I want to convert those data into
> weekly basis which means 7 days needs to be avaraged and aggregate a single
> value
>
>> dput(test)
> structure(list(locid = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1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.Label = "EC",
> class = "factor"),
>      Day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
>      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
>      31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
>      17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 1, 2,
>      3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
>      19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2,
>      3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
>      19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3,
>      4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
>      20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3,
>      4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
>      20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5,
>      6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
>      22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6,
>      7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
>      22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6,
>      7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
>      22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7,
>      8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
>      23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7,
>      8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
>      23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9,
>      10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
>      25, 26, 27, 28, 29, 30, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
>      13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
>      29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
>      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
>      ), Data = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Air", "SOIL"
>      ), class = "factor"), Value = c(53.06, 55.04, 55.94, 53.06,
>      51.08, 50, 51.08, 53.96, 48.02, 48.02, 50, 53.06, 51.98,
>      55.94, 51.98, 57.02, 55.04, 55.04, 59, 57.92, 53.96, 46.94,
>      39.92, 42.08, 46.94, 50, 60.08, 55.04, 51.98, 46.04, 53.06,
>      48.02, 51.98, 50, 55.94, 51.98, 55.04, 53.06, 59, 62.96,
>      64.04, 64.94, 66.92, 69.08, 59, 57.92, 60.98, 66.02, 64.94,
>      64.94, 55.04, 59, 53.06, 57.02, 51.08, 57.02, 59, 62.96,
>      66.92, 69.98, 71.06, 59, 62.96, 64.04, 69.08, 64.94, 62.06,
>      69.08, 69.08, 73.94, 71.96, 69.08, 69.08, 69.98, 60.98, 59,
>      62.06, 71.06, 73.94, 71.06, 66.02, 66.02, 77, 75.92, 75.92,
>      75.02, 66.92, 62.06, 66.92, 60.98, 64.04, 64.94, 69.98, 71.96,
>      75.92, 69.08, 69.98, 66.92, 68, 66.02, 69.98, 75.92, 86,
>      87.98, 87.98, 69.98, 66.92, 75.92, 80.96, 78.08, 71.96, 62.06,
>      64.04, 66.92, 64.94, 71.06, 82.04, 89.06, 91.94, 89.06, 71.96,
>      69.08, 77, 77, 82.04, 75.02, 84.92, 84.92, 78.98, 80.96,
>      82.04, 86, 80.06, 80.06, 89.96, 91.94, 98.96, 102.02, 104,
>      100.04, 95, 89.06, 80.06, 78.98, 78.08, 64.94, 75.92, 71.96,
>      75.92, 73.94, 75.02, 82.04, 78.98, 80.96, 84.02, 82.94, 80.96,
>      86, 84.02, 87.08, 93.02, 98.06, 86, 89.06, 93.92, 98.96,
>      89.96, 89.96, 86, 96.08, 96.08, 98.06, 102.92, 105.08, 93.02,
>      87.08, 91.94, 87.08, 87.98, 93.92, 91.04, 91.04, 89.96, 89.06,
>      91.94, 93.02, 84.92, 93.92, 98.96, 105.08, 107.06, 109.04,
>      100.04, 93.02, 91.94, 96.08, 93.02, 89.06, 91.94, 91.94,
>      96.08, 93.92, 82.04, 80.96, 91.94, 95, 96.08, 96.08, 102.02,
>      89.06, 87.98, 91.94, 91.94, 91.94, 93.02, 98.96, 89.06, 91.94,
>      93.02, 96.08, 93.02, 87.08, 89.96, 96.08, 98.96, 98.96, 105.08,
>      107.06, 107.96, 95, 86, 84.92, 84.92, 91.94, 93.02, 96.98,
>      95, 96.98, 96.98, 95, 102.92, 105.08, 105.98, 96.98, 86,
>      87.98, 93.92, 100.04, 102.92, 104, 102.92, 100.04, 93.02,
>      82.04, 91.94, 93.92, 87.98, 91.04, 93.92, 93.92, 89.06, 80.06,
>      80.96, 80.96, 80.96, 82.94, 87.98, 91.94, 93.02, 93.02, 95,
>      96.98, 98.96, 91.94, 91.94, 93.02, 80.06, 73.04, 75.92, 77,
>      82.94, 87.98, 86, 73.94, 66.92, 68, 71.96, 82.04, 80.96,
>      84.02, 86, 87.08, 86, 80.06, 78.08, 80.96, 84.02, 86, 86,
>      84.92, 84.92, 80.06, 84.02, 80.96, 68, 69.98, 60.08, 66.02,
>      60.98, 62.06, 59, 66.92, 69.98, 64.04, 66.02, 62.96, 62.06,
>      68, 75.92, 78.98, 75.92, 75.02, 75.02, 73.94, 69.08, 69.98,
>      68, 60.98, 64.04, 68, 62.96, 55.04, 64.94, 59, 62.06, 64.94,
>      55.04, 53.06, 55.04, 53.96, 55.94, 48.92, 44.96, 46.94, 53.06,
>      62.96, 59, 62.06, 55.94, 44.96, 51.08, 41, 50, 48.02, 53.96,
>      51.08, 46.94, 53.06, 48.92, 48.92, 51.98, 50, 51.08, 55.04,
>      60.98, 37.94, 48.02, 33.98, 41, 39.02, 41, 39.92, 44.96,
>      46.94, 44.96, 35.06, 35.06, 39.02, 39.02, 33.98, 33.08, 39.02,
>      33.08, 39.02, 37.94, 37.94, 39.92, 42.98, 44.96, 37.94, 33.98,
>      33.98, 39.92, 33.98, 33.98, 39.02, 39.92, 35.06, 37.04, 35.06,
>      37.04, 35.06, 37.94, 39.02, 39.02, 42.98, 39.02, 41, 35.06,
>      35.06, 37.94, 39.92, 46.94, 42.98, 44.96, 37.04, 46.04, 42.08,
>      39.92, 41, 42.08, 42.08)), .Names = c("locid", "Day", "Data",
> "Value"), class = "data.frame", row.names = c(NA, 422L))
> And I want the output as following:
>
>       Air week 1 Air week 2  SOIL week 1 SOIL week 2 EC 52,751429 51,568571
> 39,992 41,1285714
>
>
>
>   Many thanks in advance!!!
>
> Nico
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list