R

Sage includes R and is able to invoke it. It is possible to send lines of code, invoke commands in a standardized fashion, work with results of calculations as objects and convert those objects back into python data structures. The last point is essential to write native Sage commands that use R as backend.

Examples

sequence

sage: r.seq(1,9)    

[1] 1 2 3 4 5 6 7 8 9

Matrix

Matrix m as Sage object and R printout - then a translation to a Sage matrix.

sage: m = r.matrix(r.seq(1,12),3)

sage: type(m)
<class 'sage.interfaces.r.RElement'>

sage: m

     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

argument byrow=True

sage: m = r.matrix(r.seq(1,12),3, byrow=True)
sage: m

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12

t-Test

sage: r.t_test(r.c(1,2,3,5),r.c(1,2,4,5))


        Welch Two Sample t-test

data:  sage6 and sage7
t = -0.2, df = 5.973, p-value = 0.8481
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.311938  2.811938
sample estimates:
mean of x mean of y
     2.75      3.00