This constructs a list (R vector) of 10 consecutive integers:
| Out[1]= |  |
This uses a general vector constructor in R to construct a vector of numbers:
| Out[1]= |  |
To suppress the output, or when no output is needed, put a semicolon at the end of the R code, in which case
returns Null:
When scalars are entered, they are automatically considered vectors of length 1 by R. This is reflected in RLink:
| Out[1]= |  |
By default, real numeric quantities are interpreted as doubles:
| Out[2]= |  |
One way to have R interpret them as integers is to use R's as.integer function:
| Out[3]= |  |
This returns a more general sequence:
| Out[1]= |  |
Mathematica's equivalent follows:
| Out[2]= |  |
This computes the sine function on the sequence from the previous example (most functions in R are vectorized, just as in Mathematica):
| Out[1]= |  |
This is the same, but using explicit function mapping (sapply in R corresponds to Map in Mathematica):
| Out[2]= |  |
The second method is slower, however, just as it would be in Mathematica:
This is a slightly more complex example. Here, a function is mapped that takes a value of the independent variable and returns it back together with the value of the function (here, sine) wrapped in a vector:
| Out[1]= |  |
Here is Mathematica's equivalent for the rhs of the assignment from the previous example:
| Out[2]= |  |
You can use larger pieces of R code inside
. In such a case, however, you should wrap your statements in a block (curly braces):
| Out[1]= |  |
This splits a string, and actually returns a list rather than a vector, which is reflected in extra curly braces:
| Out[1]= |  |
Note that you have to escape the quotation marks.
This generates a random 5×5 matrix:
| Out[1]= |  |
This computes eigenvalues and eigenvectors:
| Out[2]= |  |
You can see that the result is an R list and has a non-empty attribute set (attribute names); therefore, it is represented as a general R object with a head RObject. This topic is covered in more details in "R Data Types in RLink" and the RObject reference page.
This extracts only eigenvectors:
| Out[3]= |  |
Or eigenvalues:
| Out[4]= |  |
This is the same in Mathematica (eigenvectors do not necessarily have to be the same in this case):
| Out[5]= |  |
This creates a random matrix in Mathematica:
| Out[1]= |  |
Send it to R:
| Out[2]= |  |
The result of this element-wise logical comparison is a logical vector (matrix):
| Out[3]= |  |
Test that the result is of a logical type:
| Out[4]= |  |
This lists the current objects present in your R workspace:
| Out[1]= |  |