A cheat sheet about R programming language
Rscript myscript.R .install.packages('dplyr') : Download and install a package from CRAN.library(dplyr') : Load the package into the file.dplyr::select : Use a function from the packagea <- 'bonjour' : stringa <- 6 : integerfor (variable in sequence){ Do something }while (condition){ Do something }if (condition){ Do something } else { Do something different }a == b : Are equal a > b : Greater than a >= b : Greater than or equal to is.na(a) : Is missinga != b : Not equal a < b : Less than a <= b : Less than or equal tois.null(a) : Is null function_name <- function (var){ Do something return (new_variable) }log(x) : Natural log / sum(x) : Sumexp(x) : Exponential / mean(x) : Meanmax(x) : Largest element / median(x) : Medianmin(x) : Smallest element / quantile(x) : Percentage quantilesround(x, n) : Round to n decimal placesrank(x) : Rank of elementssignif(x, n) : Round to n significant figures var(x) : The variance / cor(x, y) : Correlationsd(x) : The standard deviationl <- list(x = 1:5, y = c('a', 'b')) : A list is collection of elements which can be of different types.l[[2]] : second element of ll[1] : new list with only the first element l$x : element named xl['y'] : new list with only element named ypaste(x, y, sep = ' ') : Join multiple vectors together.paste(x, collapse = ' ') : Join elements of a vector together.grep(pattern, x) : Find regular expression matches in x.gsub(pattern, replace, x) : Replace matches in x with a string.toupper(x) : Convert to uppercase.tolower(x) : Convert to lowercase.nchar(x) : Number of characters in a string.m <- matrix(x, nrow = 3, ncol = 3) : Create a matrix from x.df[ , 2] : Select a column .df[2, ] : Select a row.df[2, 2] : Select an element.df <- data.frame(x = 1:3, y = c('a', 'b', 'c')) : A special case of a list where all elements are the same length.View(df) : See the full data frame.head(df) :See the first 6 rows.nrow(df) : Number of rows.ncol(df) : Number of columns.dim(df) : Number of columns and rows.plot(x) : plot values of x in orderplot(x, y) : plot values of x against ydf <- read.table('file.txt') / df <- read.csv('file.csv') / Read a file text or a csvwrite.table(df, ‘file.txt’) / write.csv(df, ‘file.csv’) / Write a file text or a csvNo comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.