Basic Math
|
Exponential |
|
Sum |
|
Natural log |
|
Cumulative Sum |
|
Largest element |
|
Round up |
|
Smallest element |
|
Round down |
|
Mean |
|
Median |
|
Variance |
|
Percentage quantiles |
|
Modulo |
I/O
write(data, "mydata.dat")
|
Write data as binary. |
|
Read binary data. |
Plotting
|
Plot quick overview. |
barplot(x, main="Title", xlab="x label")
|
Annotated barplot of absolute frequencies |
|
Plot ECDF. |
hist(data, prob=TRUE, breaks=30)
|
Histogram of relative frequencies (30 bins). |
|
1D-plot |
abline(a,b,col="red")
|
Add a red line with intercept a
and slope b
to the plot. |
Vectors
Creating Vectors |
|
Join elements into a vector |
|
An integer sequence (end inclusive!) |
|
Complex sequence (s. np.linspace) |
|
Repeat vector |
|
Repeat each element |
Functions |
|
Return x sorted. |
|
Return x reversed. |
|
See unique values. |
|
Length of x. |
Tables |
|
Generate absolute frequency table |
as.numeric(names(tab)); as.vector(tab)
|
Access values and frequencies of the table |
Selecting Vector Elements |
By Position |
|
The fourth element |
|
All but the fourth. |
|
Elements two to four |
|
All elements except 2 to four |
|
Elements one and five. |
By Value |
|
All elements equal to 10 |
|
All elements less than 10. |
|
Elements in the given set. |
Named Vectors |
|
Element with name 'apple'. |
Control Flow
for (variable in sequence) {...}
|
for-loop. If the loop body contains only a single line, the curly brackets can be omitted. |
|
while-loop |
if (i > 5) {
... else {
...
}
|
if-else-block |
foo = function(arg1, arg2, ...) {
...
return(var)
}
|
function |
Runs Test of Randomness
|
Compute the lengths and values of runs of equal values in a vector . |
|
Vector containing the length of each run. |
|
Vector of the same length as lengths with the corresponding values. |
Help
|
Display documentation of the command sqrt
` |
|
use quotation marks for special characters |
Miscellaneous
Printing |
|
Default print |
sprintf("Formatted %s: %.3f", object, mean)
|
Formatted print |
|
enclose an R command with brackets to directly print the result |
Random Numbers
sample(1:3,prob=c(1/6,1/3,1/2),replace=TRUE,20)
|
Draw 20 balls, labeled from 1 to 3, from box with replacement. |
|
Draw n
numbers from distribution <distr. ID>
with parameters params
|
|