3

我在将数组中的 NA 传递给 R 用于 imputeTS 包时遇到了一些问题。

假设我有这个数组:

a = Any[1, 2, 3, NaN, 5]

我想把它传递给这个:

R"""
b <- na_seadec($a, algorithm = "kalman", find_frequency = TRUE, maxgap = Inf)
"""

NaN 不会自动转换为 NA。如何将 NA 值准确传递给 RCall?

4

1 回答 1

1

NaN在 Julia 中将NaN在 R 中。如果你想要NA在 R 中,你应该missing在 Julia 中使用:

julia> x = [1, 2, NaN]
3-element Array{Float64,1}:
   1.0
   2.0
 NaN

julia> y = [1, 2, missing]
3-element Array{Union{Missing, Int64},1}:
 1
 2
  missing

julia> R"$x"
RObject{RealSxp}
[1]   1   2 NaN


julia> R"$y"
RObject{IntSxp}
[1]  1  2 NA

您可以在Julia 手册的这一部分中找到详细信息。

这是一个示例会话:

julia> R"library(imputeTS)"
RObject{StrSxp}
[1] "imputeTS"  "stats"     "graphics"  "grDevices" "utils"     "datasets"
[7] "methods"   "base"


julia> a = [1,2,3,missing,5]
5-element Array{Union{Missing, Int64},1}:
 1
 2
 3
  missing
 5

julia> R"""
       b <- na_seadec($a, algorithm = "kalman", find_frequency = TRUE, maxgap = Inf)
       """
┌ Warning: RCall.jl: Warning in na_seadec(`#JL`$a, algorithm = "kalman", find_frequency = TRUE,  :
│   No seasonality information for dataset could be found, going on without decomposition.
│               Setting find_frequency=TRUE might be an option.
└ @ RCall ~/.julia/packages/RCall/g7dhB/src/io.jl:113
RObject{RealSxp}
[1] 1 2 3 4 5
于 2020-04-25T20:23:21.240 回答