New to R - simple question of retrieving values

Apr 7, 2014 at 4:17 AM
Edited Apr 7, 2014 at 4:17 AM
I am new to this and understand c# variables but perhaps not R variables

I have a vector in R
            NumericVector x = r.CreateNumericVector(_x);
            r.SetSymbol("x", x);
and I want to get the length of it to use in C#

I am trying
        var len_x = r.Evaluate("length(x)").AsNumeric();
        NumericVector len_x = r.Evaluate("length(x)").AsNumeric();
but both don't seem to retrieve the the length of this vector into a variable I can use

Likely a very dumb question but I couldn't get the answer from the web

Thanks
Developer
Apr 7, 2014 at 2:33 PM
This looks like this should work. But the length being an integer value by nature, you'd be better off with something like
int len_x = r.Evaluate("length(x)").AsInteger()[0];
Apr 18, 2014 at 2:08 AM
Been away from this for a little bit and have a related dumb question

I've run an adf.test and put the result in a variable z

r.Evaluate("z$p.value")

I'd like to get the result of this expression into a double but I'm running into type conversion issues again and can't seem to find an example here

Thanks again for the help
Developer
Apr 18, 2014 at 1:40 PM
I think this is simply:
var pValue = r.Evaluate("z$p.value").AsNumeric()[0];
Apr 18, 2014 at 2:32 PM
Thanks again

I finally got it working in two lines like this
                NumericVector  _pVal = r.Evaluate("z$p.value").AsNumeric();
                double pVal = _pVal[0];
I guess everything is a vector (even scalars) and you just have to know how many elements are in the vector.
Hope this helps other new users like myself

This was also helpful as I was unaware of the C# type var which is implicit typing. I was under the belief that everything needed to be explicitly typed.