Passing NA to R using JULIA RCall package

95 views Asked by At

I have some problem with passing NA in an array to R for imputeTS package.

assume I have this array:

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

and I want to pass it to this:

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

NaN does not convert to NA automatically. How can I pass exactly NA value to RCall?

1

There are 1 answers

4
Bogumił Kamiński On BEST ANSWER

NaN in Julia will be NaN in R. If you want NA in R you should use missing in 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

You can find the details in this section of the Julia manual.

And here is an example session:

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