r - Reshape and mean calculation -


i have climatic data have been collected during whole year along altitude gradient. shaped that:

clim <- read.table(text="alti    year    month    week    day    meantemp    maxtemp    mintemp 350     2011    aug.     31      213    10          14         6 350     2011    aug.     31      214    12          18         6 350     2011    aug.     31      215    10          11         9 550     2011    aug.     31      213    8           10         6 550     2011    aug.     31      214    10          12         8 550     2011    aug.     31      215    8           9          7 350     2011    sep.     31      244    9           10         8 350     2011    sep.     31      245    11          12         10 350     2011    sep.     31      246    10          11         9 550     2011    sep.     31      244    7.5         9          6 550     2011    sep.     31      245    8           10         6 550     2011    sep.     31      246    8.5         9          8", header=true) 

and trying reshape data in order have 1 row per altitude , calculate mean data each month , whole year. great if shaped that:

alti    mean_year(meantemp)   mean_year(maxtemp)   mean_aug.(meantemp)   mean_aug.(maxtemp)   mean_sep.(meantemp)   [...] 350     10.333                12.667               10.667                14.3                 10                     ... 550     8.333                 9.833                8.667                 10.333               7.766                  ... 

any idea perform reshaping & calculation?

here's variation of data.table solution, requires current devel version, v1.9.5:

require(data.table) # v1.9.5+ setdt(clim) form = paste("alti", c("year", "month"), sep=" ~ ") val  = c("meantemp", "maxtemp") ans  = lapply(form, function(x) dcast(clim, x, mean, value.var = val)) reduce(function(x, y) x[y, on="alti"], ans)  #    alti meantemp_mean_2011 maxtemp_mean_2011 meantemp_mean_aug. meantemp_mean_sep. maxtemp_mean_aug. maxtemp_mean_sep. # 1:  350          10.333333         12.666667          10.666667                 10          14.33333         11.000000 # 2:  550           8.333333          9.833333           8.666667                  8          10.33333          9.333333 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -