【发布时间】:2021-09-01 18:13:23
【问题描述】:
imputa 函数返回单变量时间序列x.imp。
但是当我尝试使用 as.numeric 将其转换为向量时,我得到了 x.imp_v,它应该只是向量 (1, 2, 3)。
library(tidyverse)
library(imputeTS)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
df <- tibble(c = c("a", "a", "a", "b", "b", "b"),
d = c(1,2,3,1,2,3),
x = c(1, NA, 3,4,5,6),
y = c(1, 2, NA,4,5,6))
imputa <- function(df, c, v){
imp <- df %>%
filter(c == "a") %>%
select(d, all_of(v)) %>%
ts(.) %>%
na_interpolation(.)
}
x.imp <- imputa(df, "a", "x")
x.imp
#> Time Series:
#> Start = 1
#> End = 3
#> Frequency = 1
#> d x
#> 1 1 1
#> 2 2 2
#> 3 3 3
class(x.imp)
#> [1] "mts" "ts" "matrix"
x.imp_v <- as.numeric(x.imp)
x.imp_v
#> [1] 1 2 3 1 2 3
Created on 2021-06-16 by the reprex package (v2.0.0)
【问题讨论】:
标签: r vector time-series