1/233
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
←
Assignment operator that stores the value on the right into an object on the left.
=
Assignment operator that can also store values, but is often used for naming function arguments.
+
Adds numbers or adds corresponding elements of vectors.
-
Subtracts numbers or subtracts corresponding elements of vectors.
/
Divides numbers or divides corresponding elements of vectors.
*
Multiplies numbers or multiplies corresponding elements of vectors.
^
Raises values to a power.
:
Creates a sequence of integers from one value to another.
&
Logical AND operator; both conditions must be TRUE.
|
Logical OR operator; at least one condition must be TRUE.
==
Tests whether two values are equal.
#
Begins a comment; everything after # on the line is ignored by R.
()
Parentheses used to call a function and pass arguments.
[]
Single brackets used to subset vectors, matrices, or data frames.
[[]]
Double brackets used to extract an element from a list itself (not as a list).
$
Accesses a named column (sub-object) inside a data frame or list.
ls()
Lists all objects currently in the workspace.
rm(x)
Removes the object x from the workspace.
names(x)
Returns the names of sub-objects (such as columns in a data frame).
runif(n)
Generates n random numbers from a uniform distribution between 0 and 1.
runif(n, min, max)
Generates n random numbers uniformly between min and max.
sample(x, size)
Randomly samples values from x without replacement.
sample(x, size, replace=TRUE)
Randomly samples values from x with replacement.
c(…)
Creates a vector by concatenating values.
xvec[i]
Returns the i-th element of a vector.
xvec[c(1,3,5)]
Returns selected elements of a vector.
xvec[1:3]
Returns elements 1 through 3 of a vector.
list(…)
Creates a list that can contain mixed data types.
list1[[i]]
Returns the i-th element of a list itself.
list1[i]
Returns the i-th element of a list as a list.
list1[[i]][j]
Returns the j-th element of the vector stored at position i in a list.
matrix(x, nrow)
Creates a matrix with specified number of rows.
matrix(x, ncol)
Creates a matrix with specified number of columns.
matrix(x, byrow=TRUE)
Fills matrix row by row instead of column by column.
cbind(x, y)
Binds vectors x and y together as columns in a matrix.
t(x)
Flips a matrix over its diagonal so rows become columns and columns become rows.
xmat[i, j]
Returns element in row i and column j of a matrix.
xmat[, j]
Returns all rows of column j.
xmat[i, ]
Returns all columns of row i.
data.frame(…)
Creates a data frame that can hold mixed data types.
df[i, j]
Returns value at row i and column j of a data frame.
df[, j]
Returns column j of a data frame.
df[i, ]
Returns row i of a data frame.
df$var
Returns the column named var from a data frame.
read.csv(file, header=TRUE)
Reads a CSV file into a data frame.
head(df)
Displays the first few rows of a data frame.
dim(df)
Returns the number of rows and columns in a data frame.
str(df)
Displays the structure of a data frame and variable types.
df[df$var < x, ]
Returns rows where variable var is less than x.
is.na(x)
Tests whether values are NA.
is.na(x) == F
Tests whether values are not NA.
sqrt(x)
Takes the square root of each element of x.
exp(x)
Exponentiates each element of x.
sort(x)
Sorts values from smallest to largest.
sort(x, decreasing=TRUE)
Sorts values from largest to smallest.
plot(x, y)
Creates a scatterplot of y versus x.
plot(…, xlab, ylab, main)
Adds axis labels and a title to a plot.
plot(…, col, pch)
Changes color and plotting symbol.
boxplot(x)
Creates a boxplot of x.
hist(x)
Creates a histogram of x.
lm(formula, data)
Fits a linear regression model.
summary(model)
Displays summary statistics for a fitted model.
model$fitted.values
Returns predicted values from a regression model.
include = FALSE (chunk option)
Runs the chunk but hides both the code and its output in the knitted document.
echo = FALSE (chunk option)
Hides the code but shows the results in the knitted document.
eval = FALSE (chunk option)
Shows the code in the document but does not run it.
message = FALSE (chunk option)
Hides messages produced by code in the knitted document.
warning = FALSE (chunk option)
Hides warnings produced by code in the knitted document.
fig.cap = "…" (chunk option)
Adds a caption to a figure produced by a chunk.
fig.height (chunk option)
Sets figure height in the knitted document.
fig.width (chunk option)
Sets figure width in the knitted document.
x[x < -3]
Subsets x to values less than -3.
x[x<-3]
Assigns 3 to x (because <- is assignment), then subsets using the result; can unexpectedly overwrite x.
x[y<-3]
Assigns 3 to y, then uses that value as an index into x; x is unchanged.
x[1, ]
Subsets a matrix/data frame: row 1, all columns.
y[ ,1]
Subsets a matrix/data frame: all rows, column 1 (spacing shown around comma).
dim(x)
Returns dimensions of a matrix or data frame (number of rows and columns).
length(x)
Returns the length of a vector or list.
str(x)
Shows the structure of an object, including data types of its components.
typeof(x)
Returns the underlying type of an object (example used on a single-element list).
na.omit(x)
Removes missing values (NA) from the result.
subset(x, condition)
Returns subset meeting a condition; notes say it handles missing values automatically.
grepl(pattern, x)
Searches for a pattern within text values and returns TRUE/FALSE matches.
head(x)
Shows the first few rows/elements of an object (used for a dataset or a variable).
tail(x, n=)
Shows the last n rows/elements of an object.
levels(x)
Shows distinct levels of a factor (categorical variable).
as.numeric(x)
Converts an object to numeric; used to reveal stored numeric codes of a factor.
as.character(x)
Converts an object to character values (text).
$ (data frame)
Accesses a column (sub-object) by name in a data frame (e.g., titanic$pclass).
newcol creation via $<-
Creates a new column in a data frame by assigning to a new name (e.g., titanic$pclassChar <- …).
TRUE
Logical value used for binary (logical) type.
factor("a")
Creates a factor, R's encoding for categorical variables.
table(x)
Creates a frequency table of counts for values of x.
table(x, y)
Creates a contingency table (cross-tabulation) for two variables.
summary(x)
Summarizes an object; notes say it behaves differently depending on the variable/class.
as.factor(x)
Converts a variable to a factor.
levels(x) <- c(…)
Relabels the levels of a factor (used to rename 0/1 to "Died"/"Survived").
paste(…)
Combines pieces of text/numbers into one character string/vector.
sep="" (paste argument)
Removes the default spaces between pasted items.
round(x, digits)
Rounds numbers to a specified number of decimal places.