Year 1 R Programming - Data Wrangling

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/46

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:42 AM on 10/12/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

47 Terms

1
New cards

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').

2
New cards

Piped equivalent of filter(dataframe, sex == "F")

dataframe %>% filter(sex == "F")

3
New cards

Purpose of select()

Selects specific variables (columns) from a dataframe.

4
New cards

Basic syntax of select()

df %>% select(id, age, height)

5
New cards

Purpose of filter()

Retains only rows (observations) that meet specified logical criteria.

6
New cards

Basic syntax of filter()

df %>% filter(sex == "F")

7
New cards

Syntax for sorting a dataframe in descending order with arrange()

Place a minus sign - in front of the variable name

Example: df %>% arrange(-age)

8
New cards

Behavior of mutate() when given an existing column name

It modifies and overwrites the existing column rather than creating an additional one.

9
New cards

Syntax for computing a new column in months using mutate()

df %>% mutate(age_months = age * 12)

10
New cards

Purpose of summarise() in dplyr

Reduces multiple observations down to a single summary variable (e.g., df %>% summarise(meanage = mean(age))).

11
New cards

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.

12
New cards

Piped workflow to calculate mean age grouped by sex

df %>% group_by(sex) %>% summarise(meanage = mean(age))

13
New cards

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).

14
New cards

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.

15
New cards

Syntax for recoding 'M' to 'male' and 'F' to 'female' using if_else()

df %>% mutate(sex = if_else(sex == 'M', 'male', 'female'))

16
New cards

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.

17
New cards

select()

A dplyr verb used to select only specified variables (columns) from a dataframe.

18
New cards

filter()

A dplyr verb used to subset a dataframe by keeping only the observations (rows) that satisfy specified conditions.

19
New cards

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.

20
New cards

mutate()

A dplyr verb used to add new variables or modify existing ones; if an existing variable name is used, R will overwrite it.

21
New cards

summarise()

A dplyr verb used to collapse multiple rows of a dataframe down into a single summary variable.

22
New cards

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.

23
New cards

inner_join()

A function that merges two dataframes by retaining only the rows with matching values in both datasets.

24
New cards

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.

25
New cards

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.

26
New cards

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.

27
New cards

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.

28
New cards

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.

29
New cards

pull()

A function that extracts an individual variable from a dataframe and outputs it as a vector.

30
New cards

pluck()

A function that extracts an individual value from a vector based on its index position (e.g., passing 22 extracts the second element).

31
New cards

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.

32
New cards

rep()

A base R function used to replicate elements or patterns within a vector.

33
New cards

times argument (in rep())

The default argument in rep() that specifies how many times the entire vector sequence or pattern is repeated in full.

34
New cards

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.

35
New cards

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).

36
New cards

Syntax for recoding 'M' to 'male' and 'F' to 'female' using recode()

df %>% mutate(sex = recode(sex, 'M' = 'male', 'F' = 'female'))

37
New cards

Requirement for numeric old values inside recode()

They must have quotation marks (speech marks) around them (e.g., '1' = 'one').

38
New cards

Purpose of pivot_longer()

Reshapes data from wide format to long format by gathering columns into key-value pairs.

39
New cards

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')

40
New cards

Purpose of pivot_wider()

Reshapes data from long format to wide format by turning unique values of a key column into separate column names.

41
New cards

Syntax for widening columns from time and value with pivot_wider()

df %>% pivot_wider(names_from = time, values_from = value)

42
New cards

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.

43
New cards

Syntax to extract the 2nd value of the age column using pull() and pluck()

df %>% pull(age) %>% pluck(2)

44
New cards

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))

45
New cards

Tibble recycling behavior when a single value is provided for a column

R repeats that single value across every row/observation of the tibble.

46
New cards

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').

47
New cards

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)).