A brief cheat sheet for students learning R
= <- |
Assigns a value to an object
|
> |
greater than
|
< |
less than
|
>= |
greater than or equal to
|
<= |
less than or equal to
|
== |
exactly equal to
|
!= |
not equal to
|
!x |
not x
|
x | y |
x OR y
|
x & y |
x AND y
|
|> |
Sends something (e.g., dataframe or function output) to something else. i.e., it is an elegant way to nest tidyverse functions.
|
|
Example:
Note that summarise() is nested within filter(), which is using info from the msleep dataframe. |
|
Access a function's help file
?[function name]
|
|
Load a csv file
read.csv( "filename.csv", header = TRUE )
|
|
Install a library
install.packages("library name")
|
|
Load an installed library
library(library name)
|
|
Resize images in Google Collab
options(repr.plot.width = x, repr.plot.height = y)
|
|
Return the amount of values in x
length(x)
|
|
Return the number of rows in a dataframe
nrow(dataframe)
|
|
Return the absolute value(s) in x
abs(x)
|
|
Return the sum of all the values in x
sum(x)
|
|
Return the square-root of the value(s) in x
sqrt(x)
|
|
Return the mean of the values in x with optional arguments for trimming and removing NAs
mean(x, tr = 0, na.rm = FALSE)
|
|
Return the median of the values in x with optional arguments removing NAs
median(x, na.rm = FALSE)
|
|
Return the sample standard deviation of values in x with optional argument for removing NAs
sd(x, na.rm = FALSE)
|
|
Return the sample variance of values in x with optional argument for removing NAs
var(x, na.rm = FALSE)
|
|
Return the quartiles for x with optional argument for removing NAs
quantile(x, na.rm = FALSE)
|
|
Sort the values of x into ascending order
sort(x)
|
|
Compute the median absolute deviation of x with optional argument to remove NAs
mad(x, na.rm = FALSE)
|
|
Find NA values in x (returns TRUE/FALSE)
is.na(x)
|
|
Paste things together into a single string
paste(x, y, z, sep = "")
|
|
Create a table of counts
Examples:
table(x) table(x, y)
|
|
Create a new data frame
col_1 <- c("A", "B", "C")col_2 <- c(21, 22, NA)new_df <- data.frame(col_1, col_2)
|
|
Add a column
new_df$col_3 <- c(51, 52, 53)
|
|
Select a specific value (e.g., 52 = row 2, column 3)
new_df[2, 3]
|
|
Select a series of values (e.g., all of row 2)
new_df[2, c(1,2,3)] or new_df[2, ]
|
|
Select an entire column (e.g., column 2)
new_df$col_2 or new_df[ , 2]
|
|
Isolate values that are not NAs
new_df$col_2[!is.na(new_df$col_2)]
|
|
Used to select specific observations from a dataframe according to a rule you specify.
filter(dataframe, subset rule)
|
|
Example 1:
filter(heightData, Father < 60.1 | Father > 75.3)
|
|
Example 2:
heightData |> filter(Father < 60.1 | Father > 75.3)
|
|
Used to select specific observations from a dataframe according to a rule you specify.
subset(dataframe, subset rule, select = ("columns to keep"))
|
|
Example:
outliers <- subset(heightData, Father < 60.1 | Father > 75.3, select = c("Father"))
|
|
library(tidyverse) or library(dplyr)
|
|
Aggregate data sets into a new dataframe.
For example . . . msleep %>%
|
|
library(rcompanion)
|
|
Calculates lambda for Tukey's ladder of powers
transformTukey(x, plotit = FALSE, returnLambda = TRUE)
|
|
library(WRS2)
|
|
Winsorized variance of x
winvar(x, tr = .2)
|
|
Return the the corresponding quantile for a given cumulative probability
|
|
Normal Distribution
qnorm(probability, mean, sd)
|
|
T Distribution
qt(probability, df, lower.tail)
|
|
F Distribution
qf(probability, df1, df2, lower.tail)
|
|
Chi-Square Distribution
qchisq(probability, df, lower.tail)
|
|
Return the the corresponding cumulative probability for a given quantile.
|
|
Normal Distribution
pnorm(quantile, mean, sd)
|
|
T Distribution
pt(quantile, df, lower.tail)
|
|
F Distribution
pf(quantile, df1, df2, lower.tail)
|
|
Chi-Square Distribution
pchisq(quantile, df, lower.tail)
|
|
Histogram
ggplot(dataFrame, aes(x = Dep_Var)) + geom_histogram(colour = "black", fill = "white") |
|
Density Plot
ggplot(dataFrame, aes(x = Dep_Var)) + geom_density(colour = "black", fill = "pink", adjust = 1) |
|
Boxplot - for one sample
ggplot(dataFrame, aes(y = Dep_Var,)) + geom_boxplot()
|
|
Boxplot - for two or more samples
ggplot(dataFrame, aes(x = Indep_Var, y = Dep_Var)) + geom_boxplot()
|
|
Barplot with errorbars
ggplot(plotData, aes(x = Indep_Var, y = Dep_Var, fill = Indep_Var)) + geom_bar(stat = "identity", colour = "black") + geom_errorbar(aes(ymin = bottom_values, ymax = top_values), width = .25) |
|
Q-Q Plot For two independent samples
Remove + facet_wrap() for a single sample ggplot(dataFrame, aes(sample = Dep_Var)) + stat_qq() + stat_qq_line() + facet_wrap(~ Indep_Var)
|
|
Line Plot of Means with Two Predictors
ggplot(plotData, aes(x = PredictorA, y = Means, group = PredictorB, colour = PredictorB)) +
|
|
Scatterplot with Regression Line
ggplot(dataframe, aes(x = predictor, y = response)) +
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.