R中的错误:分配的数据必须与现有数据兼容

发布于 2025-02-13 12:56:20 字数 1273 浏览 1 评论 0原文

数据表。

dt <- data.table(currency = c("EUR","EUR","EUR","USD","USD","USD", "RON","RON","RON","RON","RON","EUR","EUR","USD","USD","USD", "RON","RON","RON","RON","RON"), date = c("2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25", "2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-03-01","2019-03-01","2019-03-01","2019-03-01","2019-03-01", "2019-03-01","2019-03-01","2019-03-01","2019-03-01","2019-03-01"), y = c("0,1","0,2","0,2","0,1","0,1","0,15","0,1","0,2","0,3","0,1","0,1","0,15","0,1","0,1","0,25","0,3","0,1","0,1","0,15","0,1","0,2")
dt

有一个

dt <- dt|>
  group_by(currency, date)  |>
  mutate(N = row_number())  |>
  ungroup()  |>
  complete(currency, date, N) |>
  arrange(date, currency, N)
dt

我 值。为此,我使用了代码:

#a table with selected columns
m <- dt[,c("currency","date","N","y")]

#add column x to the table
m$x <- na.approx(m$y)

然后我会遇到一个错误:

Error: Assigned data `na.approx(zero_rates_hist$zero_rate)` must be compatible with existing data.
x Existing data has 1654800 rows.
x Assigned data has 1654653 rows.
ℹ Only vectors of size 1 are recycled.

请帮助我!谢谢。

I have a table in data.table format in R:

dt <- data.table(currency = c("EUR","EUR","EUR","USD","USD","USD", "RON","RON","RON","RON","RON","EUR","EUR","USD","USD","USD", "RON","RON","RON","RON","RON"), date = c("2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25", "2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-02-25","2019-03-01","2019-03-01","2019-03-01","2019-03-01","2019-03-01", "2019-03-01","2019-03-01","2019-03-01","2019-03-01","2019-03-01"), y = c("0,1","0,2","0,2","0,1","0,1","0,15","0,1","0,2","0,3","0,1","0,1","0,15","0,1","0,1","0,25","0,3","0,1","0,1","0,15","0,1","0,2")
dt

I needed to use the following code:

dt <- dt|>
  group_by(currency, date)  |>
  mutate(N = row_number())  |>
  ungroup()  |>
  complete(currency, date, N) |>
  arrange(date, currency, N)
dt

Then I needed to do approximation: to create a new column "x" in the table which will include approximation of column "y" and fulfill missing values. For doing this I used code:

#a table with selected columns
m <- dt[,c("currency","date","N","y")]

#add column x to the table
m$x <- na.approx(m$y)

And then I get an error:

Error: Assigned data `na.approx(zero_rates_hist$zero_rate)` must be compatible with existing data.
x Existing data has 1654800 rows.
x Assigned data has 1654653 rows.
ℹ Only vectors of size 1 are recycled.

Help me, please! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2025-02-20 12:56:20

我决定用以下一个替换代码替换代码,现在一切都起作用了!

#add column x to the table
m <- m %>%
  mutate(x = na.approx(y, na.rm="FALSE"))

I decided to replace code with approximation with the following one and now everything works!

#add column x to the table
m <- m %>%
  mutate(x = na.approx(y, na.rm="FALSE"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文