rdotnet
rdotnet copied to clipboard
Vectors of NA values are parsed as boolean
We are using an R algorithm that usually should return a vector of doubles or NA values. Sometimes it returns a vector of NA values only ... e.g. c(NA, NA, NA, NA, NA)
For some reason, REngine.GetSymbol() would not return the NA-only vector as a list of doubles being Double.NaN (as I would expect), but as a list of Booleans being all "true".
Is this an issue with RDotNet or an issue with R's SEXPREC type? As RServe has similar behaviour, I suspect R itself.
(If my question seems weird: Sorry, I am new to R and RDotNet)
Versions used: RDotNet 1.5.5 / R 3.1.2
R has six types of vectors, but only four are commonly used. These are logical, integer, numeric, and character (plus complex and raw). Since a vector, unlike a list, has to be all of the same type R will coerce down to the most flexible format which is usually character. However, according to the docs, NA is defined to be a logical constant of length 1, so a vector of c(NA, NA, NA, NA) is automatically logical. You can verify this with R studio:
> f <- c(NA, NA, NA, NA)
> typeof(f)
[1] "logical"
However, by the coercion rules, you can go from logical->integer->double->character with logical being least flexible and character being most flexible.
So, via RStudio
> f <- as.double(c(NA, NA, NA, NA))
> typeof(f)
[1] "double"
or via R.Net
using RDotNet;
namespace RDotNetTester
{
internal class Program
{
private static void Main()
{
var engine = REngine.GetInstance();
engine.Evaluate("f <- c(NA, NA, NA, NA)");
var f = engine.GetSymbol("f");
var n = f.AsNumeric();
}
}
}
Also, I think if you use the term SEXPREC
, you are miles ahead of many R.Net users.
Thanks for your great explanation, @skyguy94 – I will know how to fix my C# code properly now.
I'm happy to help. I don't get to touch R much in my day job. Posting about it helps me internalize the fundamental details. If you want a good book for programmers using R, I recommend Advanced R
by Hadley Wickham.