3

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?

weera
  • 884
  • 1
  • 10
  • 21

1 Answers1

1

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
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • I think I found the problem. R has a mothod as.numeric() that I don't know its equivalent in Julia. The problem is: I don't define "a" statically, I have to define it as an empty array and push values to it. Probably the problem is about how to define "a" array. I tried to convert it to Float64 after all values have been pushed, but because it includes missing, I get error. Now what? – weera Apr 26 '20 at 12:57
  • 1
    Probably using e.g. `collect(Union{Float64, Missing}, a)` should give you what you want, but I would need a full code to say for sure (this is a separate thing to this question though - so if what I write does not resolve your problem maybe just post another question that is specific to the problem of conversion). – Bogumił Kamiński Apr 26 '20 at 13:39
  • OK!!! Eventually I found the answer: convert(Array{Union{Missing, Float64}}, a). I think this is absolute equivalent of as.numeric(). Thank you for your great help! – weera Apr 26 '20 at 14:06
  • 1
    `convert` is almost the same, but the equivalent is `collect` as I have suggested. The difference is that `collect` will always make a copy (like `as.numeric` in R), while `convert` will not make a copy if `a` already has `eltype` equal to `Union{Missing, Float64}`. – Bogumił Kamiński Apr 26 '20 at 17:03