1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Function of the pipe operator (%>%) in R
Passes the expression or object on its left-hand side into the first argument of the function on its right-hand side (meaning 'TAKE THIS and then DO THIS TO IT').
Piped equivalent of filter(dataframe, sex == "F")
dataframe %>% filter(sex == "F")
Purpose of select()
Selects specific variables (columns) from a dataframe.
Basic syntax of select()
df %>% select(id, age, height)
Purpose of filter()
Retains only rows (observations) that meet specified logical criteria.
Basic syntax of filter()
df %>% filter(sex == "F")
Syntax for sorting a dataframe in descending order with arrange()
Place a minus sign - in front of the variable name
Example: df %>% arrange(-age)
Behavior of mutate() when given an existing column name
It modifies and overwrites the existing column rather than creating an additional one.
Syntax for computing a new column in months using mutate()
df %>% mutate(age_months = age * 12)
Purpose of summarise() in dplyr
Reduces multiple observations down to a single summary variable (e.g., df %>% summarise(meanage = mean(age))).
Effect of group_by() on a dataframe when called alone
It introduces 'invisible' grouping metadata without altering the visible structure until an action like summarise() or mutate() is performed.
Piped workflow to calculate mean age grouped by sex
df %>% group_by(sex) %>% summarise(meanage = mean(age))
Definition and row retention of inner_join()
Joins two dataframes together keeping only the rows that have matching values in both tables across the key column(s).
Purpose of the by argument in inner_join(df1, df2, by = "id")
Specifies the shared key variable used to match rows. Omitting it causes R to guess, which can produce unintended joins.
Syntax for recoding 'M' to 'male' and 'F' to 'female' using if_else()
df %>% mutate(sex = if_else(sex == 'M', 'male', 'female'))
Pipe (%>%)
A syntax operator that directs R to take the preceding object and insert it into the subsequent function as the first argument, making code more human-readable.
select()
A dplyr verb used to select only specified variables (columns) from a dataframe.
filter()
A dplyr verb used to subset a dataframe by keeping only the observations (rows) that satisfy specified conditions.
arrange()
A dplyr verb used to sort dataframe rows in order based on one or more variables; preceding a variable with a minus sign (such as -age) reverses the sorting order.
mutate()
A dplyr verb used to add new variables or modify existing ones; if an existing variable name is used, R will overwrite it.
summarise()
A dplyr verb used to collapse multiple rows of a dataframe down into a single summary variable.
group_by()
A dplyr function that groups a dataframe by one or more variables; these groupings are invisible until another operation (most commonly summarise()) is executed.
inner_join()
A function that merges two dataframes by retaining only the rows with matching values in both datasets.
by argument (in inner_join())
An argument that explicitly declares the shared key variable between two dataframes; omitting it forces R to guess, which can produce errors.
if_else()
A recoding function used inside mutate() that evaluates a test condition and returns one value if TRUE and an alternate value if FALSE.
recode()
A function used within mutate() to replace existing values with new values using the syntax original_value = new_value, requiring speech marks around original values even when numeric.
pivot_longer()
A tidyr function that transforms data from wide format to long format by consolidating selected columns into names_to and values_to columns.
pivot_wider()
A tidyr function that transforms data from long format to wide format, turning unique values from names_from into column headers and filling them with values from values_from.
pull()
A function that extracts an individual variable from a dataframe and outputs it as a vector.
pluck()
A function that extracts an individual value from a vector based on its index position (e.g., passing 2 extracts the second element).
tibble()
A function used to construct a dataframe; if a variable is supplied with only a single value, it automatically repeats that value across every observation.
rep()
A base R function used to replicate elements or patterns within a vector.
times argument (in rep())
The default argument in rep() that specifies how many times the entire vector sequence or pattern is repeated in full.
each argument (in rep())
An argument in rep() that specifies how many times each individual element of a vector is repeated before proceeding to the next element.
seq()
A function used to create a regular sequence of numbers defined by a starting point (from), an end point (to), and an increment step (by, which can be negative).
Syntax for recoding 'M' to 'male' and 'F' to 'female' using recode()
df %>% mutate(sex = recode(sex, 'M' = 'male', 'F' = 'female'))
Requirement for numeric old values inside recode()
They must have quotation marks (speech marks) around them (e.g., '1' = 'one').
Purpose of pivot_longer()
Reshapes data from wide format to long format by gathering columns into key-value pairs.
Syntax to reshape columns T1 through T2 into time and value with pivot_longer()
df %>% pivot_longer(cols = T1:T2, names_to = 'time', values_to = 'value')
Purpose of pivot_wider()
Reshapes data from long format to wide format by turning unique values of a key column into separate column names.
Syntax for widening columns from time and value with pivot_wider()
df %>% pivot_wider(names_from = time, values_from = value)
Difference in output between select(df, age) and pull(df, age)
select() outputs a dataframe/tibble with one column, whereas pull() extracts the column as a raw atomic vector.
Syntax to extract the 2nd value of the age column using pull() and pluck()
df %>% pull(age) %>% pluck(2)
Syntax to create a tibble with columns id, sex, and height
tibble(id = 1:4, sex = c('M', 'F', 'F', 'M'), height = c(1.56, 1.87, 1.34, 1.90))
Tibble recycling behavior when a single value is provided for a column
R repeats that single value across every row/observation of the tibble.
Difference between times = 2 and each = 2 in rep(c('M', 'F'), ...)
times = 2 repeats the entire sequence ('M', 'F', 'M', 'F'); each = 2 repeats each individual element before proceeding ('M', 'M', 'F', 'F').
Syntax to generate a sequence from 0.5 to 12.5 in steps of 4 using seq()
seq(from = 0.5, to = 12.5, by = 4) (producing c(0.5, 4.5, 8.5, 12.5)).