Question
R studio The goal is to convert this dataset to the tidy format shown below. You will solve this in two different ways in (b)
R studio
The goal is to convert this dataset to the tidy format shown below. You will solve this in two different ways in (b) and (c)
Location | Year | Employee_Contribution | Employer_Contribution |
United States | 2013 | 1170 | 4401 |
United States | 2014 | 1234 | 4598 |
United States | 2015 | 1255 | 4708 |
United States | 2016 | 1325 | 4776 |
United States | 2017 | 1415 | 4953 |
United States | 2018 | 1427 | 5288 |
Alabama | 2013 | 1379 | 3825 |
Alabama | 2014 | 1362 | 4164 |
Alabama | 2015 | 1228 | 4505 |
Alabama | 2016 | 1510 | 4026 |
Alabama | 2017 | 1593 | 4482 |
Alabama | 2018 | 1453 | 4636 |
-
Approach 1: Pivot and separate the two sets of columns (Employee_Contribution, Employer_Contribution) and create two temporary tables. Then join the two tables. Specifically, do the following steps. Show your code for each step and any summary output from the code.
-
Read the .csv file using read_csv (NOT read.csv) and store it in a table.
-
Select only the Location column and the columns containing the word Employee
-
Pivot the data in the Employee columns into a pair of names_to and values_to columns called Year and Employee_Contribution
-
The Year column has values such as 2013__Employee_Contribution. It should contain only the year (e.g., 2013). Use separate() to separate the values into the year component and discard the remaining portion. [Hint: use __ as the separator. Specifying NA in the into parameter discards the variable.]
-
Store the result of the above pipeline in a table.
-
Repeat the above steps for the Employer columns and store the result in another table.
-
Join the two tables. Check that the resulting table matches the desired tidy format.
-
-
Approach 2: Pivot all columns into two names_to columns, then pivot again! Specifically, do the following steps. Show your code for each step.
-
Read the .csv file using read_csv (NOT read.csv) and store it in a table.
-
Pivot_longer all columns (except Location) into two names_to columns. This requires a names_sep to be specified. Read the help for pivot_longer().
-
Pivot_wider a pair of columns from the previous step. Check that the resulting table matches the desired tidy format.
-
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started