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
有一个
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我决定用以下一个替换代码替换代码,现在一切都起作用了!
I decided to replace code with approximation with the following one and now everything works!