gpt4 book ai didi

rbind 函数更改我的条目

转载 作者:行者123 更新时间:2023-12-02 07:40:28 27 4
gpt4 key购买 nike

我有两个要附加在一起的 data.frames。一个 data.frame 保存过去的数据,另一个 data.frame 保存 future 的预测数据。例如:

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")

我使用了 rbind(past,future) 并且由于某种原因 future data.frame 中的 Period 条目都被重命名。它看起来像:

> 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

为什么我的日期被重命名为随机数?

最佳答案

dput(past) 的输出中可以看出, past$Period是字符,而不是日期。 str(past)str(future)会告诉你他们是不同的。

> 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 ...

data.frame 的列中的所有元素必须是同一类型,所以 future$Period列被转换...不知道为什么它没有被正确转换,因为:

> as.character(future$Period)
[1] "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01"
[5] "2012-11-01" "2012-12-01"

所以解决方案是任一:

  1. secret future$Period转字符:future$Period <-
    as.character(future$Period)
  2. secret past$Period迄今为止:past$Period <-
    as.Date(past$Period)

取决于您想要的输出。

关于rbind 函数更改我的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11785710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com