I have two data.frames that I want to append together. One data.frame holds past data, and the other data.frame holds future projected data. For example:
past <- structure(list(Period = c("2010-01-01", "2010-02-01", "2010-03-01",
"2010-04-01", "2010-05-01", "2010-06-01"), Count = c(3379221317,
3379221317, 3791458246, 4182424930, 4891432401, 5116360744)),
.Names = c("Period", "Count"), row.names = c(NA, 6L), class = "data.frame")
future <- structure(list(Period = structure(c(15522, 15553, 15584, 15614,
15645, 15675), class = "Date"), Count = c(11051746713.9419, 11385578654.4160,
10864084232.2216, 11336164255.3271, 11121729107.9691, 12321341701.3780)),
.Names = c("Period", "Count"), row.names = c(NA, 6L), class = "data.frame")
I used rbind(past,future) and for some reason the Period entries from the future data.frame are all renamed. It looks like:
> rbind(past,future)
Period Count
1 2010-01-01 3379221317
2 2010-02-01 3379221317
3 2010-03-01 3791458246
4 2010-04-01 4182424930
5 2010-05-01 4891432401
6 2010-06-01 5116360744
7 15522 11051746714
8 15553 11385578654
9 15584 10864084232
10 15614 11336164255
11 15645 11121729108
12 15675 12321341701
Why are my dates renamed to random numbers?
As you can see from the output of dput(past), past$Period is character, not a Date. str(past) and str(future) would have shown you they were different.
> str(past)
'data.frame': 6 obs. of 2 variables:
$ Period: chr "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" ...
$ Count : num 3.38e+09 3.38e+09 3.79e+09 4.18e+09 4.89e+09 ...
> str(future)
'data.frame': 6 obs. of 2 variables:
$ Period: Date, format: "2012-07-01" "2012-08-01" ...
$ Count : num 1.11e+10 1.14e+10 1.09e+10 1.13e+10 1.11e+10 ...
All the elements in the column of a data.frame must be the same type, so the future$Period column gets converted... not sure why it doesn't get converted correctly though, since:
> as.character(future$Period)
[1] "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01"
[5] "2012-11-01" "2012-12-01"
So the solution is to either:
future$Period to character: future$Period <-
as.character(future$Period)past$Period to Date: past$Period <-
as.Date(past$Period)depending on your desired output.
They are not random. Matrix elements are required to have no attributes, so factors, Dates and POSIXt items become their numeric representations if coerced to matrices. There is an rbind.data.frame but you have apparently offered rbind an object that ended up calling the non-data.frame version of that function. You will need to offer dput() from both arguments to get a better answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With