How to merge 2 time series data (wide table) into 1 data frame (wide table)?

时间:2017-12-18 06:39:54

标签: r dataframe time-series

I am using RStudio and I have the following 2 outputs from my R codes:

actual_2017 <- tail(mydata,12)
> actual_2017
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
2017 5980 5341 5890 5596 5753 5470 5589 5545 5749 5938 5844 5356

> predicted_2017 <- head(pred1,12)
> predicted_2017
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
 2017 5762 5275 5733 5411 5406 4954 5464 5536 5805 5819 5903 5630

> str(actual_2017)
 Time-Series [1:12] from 2017 to 2018: 5980 5341 5890 5596 5753 5470 5589 5545 5749 5938 ...

 > str(predicted_2017)
   Time-Series [1:12] from 2017 to 2018: 5763 5275 5734 5412 5407 ...

I want to merge actual_2017 and predicted_2017 into a data frame but keeping that wide table format.

Let's say this is the data frame I'm after:

         Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
2017(A) 5980 5341 5890 5596 5753 5470 5589 5545 5749 5938 5844 5356
2017(P) 5762 5275 5733 5411 5406 4954 5464 5536 5805 5819 5903 5630  

How can I achieve this? Also, I will need the (A) and (P) next to 2017 so that the table becomes clear about its contents.

2 个答案:

答案 0 :(得分:1)

Hope this helps!

#sample data (I cooked my own sample data, hope you don't mind it!)
actual_2017 <- ts(tail(AirPassengers,12), start = 2017, frequency = 12) 
predicted_2017 <- ts(head(AirPassengers, 12), start = 2017, frequency = 12) 

#merge both timeseries data
df <- rbind(actual_2017, predicted_2017)
colnames(df) <- month.abb
rownames(df) <- gsub("(^\\w).*(\\d{4}$)","\\2 (\\1)",rownames(df))
df

Output is:

         Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2017 (a) 417 391 419 461 472 535 622 606 508 461 390 432
2017 (p) 112 118 132 129 121 135 148 148 136 119 104 118

答案 1 :(得分:1)

By using, t (transpose), rbind, names, rownames etc. you can achieve what you want:

actual_2017 <- data.frame(t(c(5980, 5341, 5890, 5596, 5753, 5470, 5589, 5545, 5749, 5938, 5844, 5356)))
names(actual_2017 ) <- c("Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
rownames(actual_2017) <- "2017"
actual_2017
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
2017 5980 5341 5890 5596 5753 5470 5589 5545 5749 5938 5844 5356

predicted_2017 <- data.frame(t(c(5762, 5275, 5733, 5411, 5406, 4954, 5464, 5536, 5805, 5819, 5903, 5630)))
names(predicted_2017 ) <- c("Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
rownames(predicted_2017) <- "2017"
predicted_2017
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
2017 5762 5275 5733 5411 5406 4954 5464 5536 5805 5819 5903 5630

merged <- rbind(actual_2017, predicted_2017)
rownames(merged) <- c("2017 (A)", "2017 (P)")
merged
          Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
2017 (A) 5980 5341 5890 5596 5753 5470 5589 5545 5749 5938 5844 5356
2017 (P) 5762 5275 5733 5411 5406 4954 5464 5536 5805 5819 5903 5630