Basic Math
|
Exponential |
|
Sum |
|
Natural log |
|
Cumulative Sum |
|
Largest element |
|
Round up |
|
Smallest element |
|
Round down |
|
Modulo |
I/O
write(data, "mydata.dat")
|
Write data as binary. |
|
Read binary data. |
|
Current working directory |
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. |
boxplot(data1, data2, ... ,range=1.5)
|
Plot boxplots of one or more data sequences in one window. range
determines the extend of the whiskers. Default range=1.5
, i.e. 1.5 x IQR |
|
QQ-Plot against standard normal distribution |
|
QQ-Plot against any R-standard distribution |
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. |
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 |
|
Invoke text editor on R object |
|
Load package MASS |
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
|
(see Distributions in R for more details) |
Tables
|
get absolute frequencies of values |
as.numeric(tab); as.vector(tab)
|
Extract values and their absolute frequencies from table |
|
Compute relative frequencies |
Characteristics of data sequences
|
Arithmetic mean of the data sequence |
|
Variance |
|
Median |
|
Quantile. type=7
is the default computation algorithm, i.e. the function returns the value at position k=1+p(n-1)
, if this is an integer. Otherwise, R computes a weighted mean of the two neighboring integers |
|
General inverse function of the ECDF (smallest p-quantile). Largetst p-quantile can be obtained indirectly by slightly increasing p |
|
Overview of important measures |
Data sets
Interacting with data sets |
col_1 = data$col_1_name
|
Access column data |
I/O |
data = read.csv("file.csv", header=FALSE, sep="")
|
Read csv (function arguments similar to that used in pandas) |
write.csv(data, "data.csv", row.names=FALSE, sep=" ")
|
Write data set as csv |
|