select() and rename()

Classwork 6

Author

Byeong-Hak Choe

Published

October 7, 2024

Modified

October 7, 2024

For this Classwork 6, consider the following data.frame, nyc_payroll_new.

library(tidyverse)
nyc_payroll_new <- read_csv("https://bcdanl.github.io/data/nyc_payroll_2024.csv")


For detailed descriptions of the variables in this data.frame, please refer to the following link: Citywide Payroll Data (Fiscal Year)


Question 1

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 the data.frame nyc_payroll_new.

Answer:

Here, we use the select() function from one of the packages in tidyverse to choose only the specified variables (Fiscal_Year, Agency_Name, First_Name, Last_Name, and Base_Salary) from the nyc_payroll_new data.frame. The result is assigned to a new data.frame name df containing only these variables, reducing the dataset for further analysis.



Question 2

Given the data.frame df with a variable named Agency_Name, how would you rename it to Agency?

Answer:

The rename() function allows us to change the name of an existing variable. In this case, we rename the Agency_Name variable to Agency in the df data.frame. The result is a new data.frame, df_renamed, where Agency_Name is now simply Agency.



Question 3

How would you remove the Fiscal_Year variable using select()?

Answer:

To remove a column from a data.frame, we use the select() function with the - sign before the variable name. In this case, select(-Fiscal_Year) removes the Fiscal_Year variable from the df data.frame, resulting in df_dropped, which contains all variables except Fiscal_Year.



Discussion

Welcome to our Classwork 6 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 6.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) or peer classmate (@GitHub-Username) regarding the Classwork 6 materials or need clarification on any points, don’t hesitate to ask here.

Let’s collaborate and learn from each other!

Back to top