Github repo cloud-examples: https://github.com/sagemath/cloud-examples
License: MIT
RPy2
RPy2 is a Python module for interacting with R from Python. It exposes functions, packages and more in Python and allows to reference them. Dots in R functions are automatically converted to underscores. Additionally, data conversions for various types can be enabled - first and foremost for NumPy arrays.
Referencing R functions
RPy2’s robjects
(or sometimes just imported as ro
) exposes the R instance as .r
.
It is rather easy to get hold of functions and reference them from Python:
Evaluating R code directly
Vectorization
First, enable automatic conversion from NumPy arrays to R arrays. Then, even the custom function works out of the box.
Types of Vectors
[ ]
and [[ ]]
are rx
and rx2
Bonus: Factors
Packages
The idea is to get hold of a reference to a package. The reference is like a module-namespace and populated with all the members.
Datasets
Use importr
from rpy2.robjects.packages
to get hold of the dataset package.
Then fetch a dataset and retrieve the named entry to get hold of the dataframe.
Example: lm
Plot
Advanced: PCA
R Help on ‘sum’sum package:base R Documentation
Sum of Vector Elements
Description:
‘sum’ returns the sum of all the values present in its arguments.
Usage:
sum(..., na.rm = FALSE)
Arguments:
...: numeric or complex or logical vectors.
na.rm: logical. Should missing values (including ‘NaN’) be removed?
Details:
This is a generic function: methods can be defined for it directly
or via the ‘Summary’ group generic. For this to work properly,
the arguments ‘...’ should be unnamed, and dispatch is on the
first argument.
If ‘na.rm’ is ‘FALSE’ an ‘NA’ or ‘NaN’ value in any of the
arguments will cause a value of ‘NA’ or ‘NaN’ to be returned,
otherwise ‘NA’ and ‘NaN’ values are ignored.
Logical true values are regarded as one, false values as zero.
For historical reasons, ‘NULL’ is accepted and treated as if it
were ‘integer(0)’.
Value:
The sum. If all of ‘...’ are of type integer or logical, then the
sum is integer, and in that case the result will be ‘NA’ (with a
warning) if integer overflow occurs. Otherwise it is a length-one
numeric or complex vector.
*NB:* the sum of an empty set is zero, by definition.
S4 methods:
This is part of the S4 ‘Summary’ group generic. Methods for it
must use the signature ‘x, ..., na.rm’.
‘plotmath’ for the use of ‘sum’ in plot annotation.
References:
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
Language_. Wadsworth & Brooks/Cole.
See Also:
‘colSums’ for row and column sums.