Question

I'm trying to transform my data like this

"Months  2005    2006    2007    2008    2009    2010"
Jan  28.1    5.8     22.9    64.2    70.1    85.9
Feb  41.4    85.1    149.2   31.0    142.1   36.9
Mar  153.2   145.9   101.6   308.8   171.8   57.5
Apr  57.0    212.4   170.3   244.5   278.9   132.9
May  154.9   119.9   57.8    128.8   212.7   55.6
jun  158.6   81.3    160.8   94.0    115.3   63.3
Jul  22.6    27.2    29.4    80.9    82.7    30.9

My present data (about 6 stations: ABCDEF) looks like this format :

station  element Year Month 
1 A 41.4  2005         1
2 B 158.6 2005         2 
3 C 23    2006         3
4 D 234   2007         4 
5 E 0     2010         5 
6 F 0     2010         6 
Was it helpful?

Solution

It sounds like you're saying you want to go from the bottom data structure to the top one. If that's the case, you can try xtabs (if sum is all that you would need as an aggregation function) or dcast from "reshape2":

library(reshape2)
dcast(mydf, Month ~ Year, value.var="element", fun.aggregate=sum)
#   Month  2005 2006 2007 2010
# 1     1  41.4    0    0    0
# 2     2 158.6    0    0    0
# 3     3   0.0   23    0    0
# 4     4   0.0    0  234    0
# 5     5   0.0    0    0    0
# 6     6   0.0    0    0    0

xtabs(element ~ Month + Year, mydf)
#      Year
# Month  2005  2006  2007  2010
#     1  41.4   0.0   0.0   0.0
#     2 158.6   0.0   0.0   0.0
#     3   0.0  23.0   0.0   0.0
#     4   0.0   0.0 234.0   0.0
#     5   0.0   0.0   0.0   0.0
#     6   0.0   0.0   0.0   0.0

Changing the numeric value of "Month" to the short text equivalent should be a pretty straightforward substitution activity using [ and month.abb and can be done before or after transforming the data.


This assumes that "mydf" is defined as:

mydf <- structure(list(station = c("A", "B", "C", "D", "E", "F"), 
                       element = c(41.4, 158.6, 23, 234, 0, 0), 
                       Year = c(2005L, 2005L, 2006L, 2007L, 2010L, 2010L), 
                       Month = 1:6), 
                  .Names = c("station", "element", "Year", "Month"), 
                  class = "data.frame", 
                  row.names = c("1", "2", "3", "4", "5", "6"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top