library(tidyverse)
nyc_payroll_new <- read_csv("https://bcdanl.github.io/data/nyc_payroll_2025.csv")Classwork 7
Data Transformation with R - select() and rename()
Data
Analytics
On this page
Complete the questions in this Classwork and submit your responses on the corresponding Brightspace page.
Fill in every ____ below. In the code, a blank may stand for a function, variable, operator, or value. Copy your completed code into your R script and run it after loading the payroll data.
Complete Questions 1–3 in order. Question 1 creates df; Questions 2 and 3 update it. Each completed command saves its result to df.
Consider the following data.frame, nyc_payroll_new.
For detailed descriptions of the variables in this data.frame, please refer to the following link:
Question 1
Fill in the blanks to create a new data.frame, df, that keeps only the following five variables—Fiscal_Year, Agency_Name, First_Name, Last_Name, and Base_Salary—from nyc_payroll_new.
df <- ____ |>
____(Fiscal_Year, Agency_Name, First_Name, Last_Name, Base_Salary)Use select() to keep the five requested columns in the order shown, and assign the result to df.
df <- nyc_payroll_new |>
select(Fiscal_Year, Agency_Name, First_Name, Last_Name, Base_Salary)
names(df)[1] "Fiscal_Year" "Agency_Name" "First_Name" "Last_Name" "Base_Salary"
names(df) returns the column names, confirming that df contains only the five requested variables.
Question 2
Using the df from Question 1, fill in the blank to rename Agency_Name to Agency. Save the updated data.frame back to df.
df <- df |>
rename(____ = Agency_Name)In rename(), put the new column name on the left of = and the current name on the right. Assign the result back to df so the change is saved for Question 3.
df <- df |>
rename(Agency = Agency_Name)
names(df)[1] "Fiscal_Year" "Agency" "First_Name" "Last_Name" "Base_Salary"
Agency now appears in the column names where Agency_Name appeared before; the other columns stay the same.
Question 3
Using the df from Question 2, fill in the blank to remove Fiscal_Year with select(). Save the updated data.frame back to df.
df <- df |>
select(____ Fiscal_Year)A minus sign before a column name tells select() to remove that column. Assigning the result back to df keeps the updated table.
df <- df |>
select(-Fiscal_Year)
names(df)[1] "Agency" "First_Name" "Last_Name" "Base_Salary"
Fiscal_Year is no longer among the column names. The remaining columns are Agency, First_Name, Last_Name, and Base_Salary.