Splitting sequentially a data frame R -


I have such a data frame

  V1V 2V 3V 4 months 1-191-1 1989 1989-1991 1 1989 4-192 2 1989 3 2 7 3 1 1990 4 4 8 2 2 1990 3 6292 2 1990 4 7 2 2 1990 5 8 4 2 2 1990  

Where the first lines indicate the value of volume A in cell 1,2,3,4 and the last two columns give months and years. To calculate the monthly average of A for each cell, what I would like to do and to do it with the list is

  V1 1989  

Many thanks

I was still expecting a little more precise in my question about what your desired product really is, but since you have not updated that part, I will provide two options.

Option 1

Total seems a pretty straightforward tool for this task, especially if sticking to the "broad" format will be fine for your needs. . Total (Year + Month, MEDF, Median) # Year Month V1V 2V 3V4 # 1 1989 1 1 -1.00 9.00 1 # 2 1990 1 3 2.00 7.00 3 # 3 1989 2 4 -1.00 9.00 1 # 4 1990 2 4 6.25 5.25 2

Option 2

If you pre-fetch your data in a "long" format, You should find the "Rezep 2" package which can reset and consolidate in a few steps.

  Library (rhesus 2) mydfL & lt; - Melt (Mydf, id.vars = c ("year", "month")) ## The next step is purely cosmetic ... mydfL $ month & lt; - factor (month.b [mydfL $ month], month.abb, ordered = TRUE head (mydfL) # years month variable value # 1 1989 Jan V 1 1 # 2 1989 Jan V 1 1 # 3 February 1989 v 1 4 # 4 1990 Jan V 1 3 # 5 February 1990 V1 4 # 6 1990 Feb V1 3 # # This is the real aggregation and revival step ... out & lt; - dcast (mydfL, variable + year ~ month, value.var = "value", funny.graaget = mean) # variable year Jan Feb # 1 V1 1989 1 4.00 # 2V 1 1990 3 4.00 # 3 V2 1989-1 -1.00 # 4V2 1990 2 6.25 # 5V3 1989 9.00 # 6V3 1990 7 5.25 # 7V4 1989 1 1.00 # 8V4 1990 3 2.00  
< / Html>

Comments