kjl
kjl

Reputation: 341

sagemath: convert R Element to float

How can I extract a numerical part of an R Element?

I'm using the R interface in sagemath. And I would like to get float or int values.

I have an R Element, that is an object of class sage.interfaces.r.RElement, that contains a value that I would like to extract.

For example, given the following element:

 x = [1] 6.5

How may I extract the 6.5 value as a float?

float(x) 

does not work.

float(str(x).split()[1])

seems excessively crude.

For any interested in generating the example R Element, it my be done in a sagemathcell with the following code:

aList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
fences = r.quantile(aList)
x = fences[[3]]
print (type(x))
print (x)

Upvotes: 4

Views: 1141

Answers (1)

Samuel Lelièvre
Samuel Lelièvre

Reputation: 3453

You can convert x back to a float in Sage with x.sage().

If x is defined as in the question:

sage: s = x.sage()
sage: s
6.5
sage: type(s)
<type 'float'>

Upvotes: 2

Related Questions