There are a lot of built-in function in R. R matches your input parameters with its function arguments, either by value or by position, then executes the function body. Function arguments can have default values: if you do not specify these arguments, R will take the default value.
- General function
- Maths function
- Statistical function
General functions
diff() function
If you work on time series, you need to stationary the series by taking their lag values. A stationary process allows constant mean, variance and autocorrelation over time. This mainly improves the prediction of a time series. It can be easily done with the function diff(). We can build a random time-series data with a trend and then use the function diff() to stationary the series. The diff() function accepts one argument, a vector, and return suitable lagged and iterated difference.
length() function
In many cases, we want to know the length of a vector for computation or to be used in a for loop. The length() function counts the number of rows in vector x. The following codes import the cars dataset and return the number of rows.
Math functions
R has an array of mathematical functions.
Operator | Description |
---|
abs(x) | Takes the absolute value of x |
log(x,base=y) | Takes the logarithm of x with base y; if base is not specified, returns the natural logarithm |
exp(x) | Returns the exponential of x |
sqrt(x) | Returns the square root of x |
factorial(x) | Returns the factorial of x (x!) |
Statistical functions
Operator | Description |
---|
mean(x) | Mean of x |
median(x) | Median of x |
var(x) | Variance of x |
sd(x) | Standard deviation of x |
scale(x) | Standard scores (z-scores) of x |
quantile(x) | The quartiles of x |
summary(x) | Summary of x: mean, min, max etc.. |