简体   繁体   中英

Reshape data.frame from long to wide on time series

I have a data set of 13 stations with more than 20 thousands daily (around 50 years) observations. Each station is placed in single column along with year, month and date on left side as below! Columns are as (year, month, data, name of the stations (a,b,c,d......m).

 Year Month Date a b c d e f g h i j k l m 1961 1 1 0.2 0 0 0.01 0 0 0 0 0 0.04 1961 1 2 0.05 0 0 0 0 0 9.398 1.27 5.842 0.01 1961 1 3 0 0 0 0 0 0 1.27 0 0.01 1961 1 4 0 0 0 0 0 0 0 0 0 0 1961 1 5 0 0 0 0 0 0 0 0 0 0 1961 1 6 0 0 0 0 0 0 0 0 0 0 1961 1 7 0 0 0 0 0 0 0 0 0 0 1961 1 8 0 0 0 0 0 0 0 0 0 0 

And I have to reorder in this dimension by keeping one month in a row and years as column and columns are showing as Year, month, days of month(1,2,3,4,5,........30 or 31 as per month). It should be execute for each station explicitly.

 Year Month 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1960 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1960 1 22 11 0 0 0.3 0 0 0 0 0 0 0 0 0 0 0 0 0 

For this purpose, I tried aggregate() but could not figure it out. Thanks in advance for any sort of help.

Reshape data from long to wide. I solved it in this way, make subset of each column with year, month and date then execute this code for each.

library(reshape) 
reshape(object, idvar =c("Year","Month"), timevar ="Date", direction = "wide")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM