R programming is a language and environment for statistical computing and graphics. It is widely used among statisticians and data miners for developing statistical software and data analysis. R is highly extensible and provides a wide variety of statistical and graphical techniques, including linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering, and more.
Main Functions in R Programming
Basic Mathematical Functions:
sum(x)
: Returns the sum of all the elements ofx
.prod(x)
: Returns the product of all the elements ofx
.min(x)
: Returns the minimum value ofx
.max(x)
: Returns the maximum value ofx
.mean(x)
: Returns the mean (average) ofx
.median(x)
: Returns the median ofx
.sd(x)
: Returns the standard deviation ofx
.var(x)
: Returns the variance ofx
.
Data Manipulation Functions:
subset(x, condition)
: Returns subsets ofx
that meet the condition.merge(x, y, by)
: Merges two data frames by common columns or row names.apply(x, margin, FUN)
: Applies a function to the margins of an array or matrix.lapply(x, FUN)
: Applies a function to each element of a list and returns a list.sapply(x, FUN)
: Applies a function to each element of a list and returns a vector or matrix.
Data Aggregation and Reshaping:
aggregate(x, by, FUN)
: Splits the data into subsets, computes summary statistics, and returns the result in a convenient form.tapply(x, INDEX, FUN)
: Applies a function to each cell of a ragged array, which is grouped by the given factors.reshape(data, idvar, timevar, direction)
: Reshapes a data frame between long and wide formats.
Statistical Functions:
cor(x, y)
: Calculates the correlation between two vectors or matrices.cov(x, y)
: Calculates the covariance between two vectors or matrices.lm(formula, data)
: Fits linear models.glm(formula, family, data)
: Fits generalized linear models.
Plotting and Graphics:
plot(x, y)
: Creates a scatter plot or other type of plot depending on the type of arguments.hist(x)
: Creates a histogram of the data.boxplot(x)
: Creates a boxplot of the data.barplot(height)
: Creates a bar plot with vertical or horizontal bars.ggplot(data, aes)
: Creates a complex multi-layered graph using theggplot2
package.
Data Import and Export:
read.table(file)
: Reads a file in table format and creates a data frame from it.read.csv(file)
: Reads a CSV file and creates a data frame.write.table(x, file)
: Writes a data frame or matrix to a file.write.csv(x, file)
: Writes a data frame to a CSV file.
Miscellaneous Functions:
str(object)
: Compactly displays the structure of an object.summary(object)
: Provides a summary of an object, such as a statistical summary for a data frame or model.head(x, n)
: Returns the firstn
rows of a data frame or matrix.tail(x, n)
: Returns the lastn
rows of a data frame or matrix.
These functions cover a wide range of operations in R, making it a powerful tool for data analysis and statistical computing