I was handed down a script which transforms and pivots some data. After fixing the code, I finally got it to pivot my data but it only seems to write the first two lines, the column name and the first row of data.
Here is my code:
# libraries
library(dplyr)
library(tidyr)
library(readxl)
library(openxlsx)
library(rlang)
# Use this for leavers survey
# change the file path "<path>/NEW - leavers data 2019 2020.xlsx"
inputFilepath <- "C:/Users/bangathj/OneDrive - Centrica/Documents/Projects/Automation/Excel R Transformation Project/Leavers Survey/SCH0132 - Exit Interview Responses 2022-07-15 04_30 BST.xlsx"
outputFilepath <- "C:/Users/bangathj/OneDrive - Centrica/Documents/Projects/Automation/Excel R Transformation Project/Leavers Survey/SCH0132 - Exit Interview Responses 2022-07-15 04_30 BST Reshaped.xlsx"
# C:/Users/bangathj/OneDrive - Centrica/Documents/Projects/Automation/Excel R Transformation Project/Leavers Survey/SCH0132 - Exit Interview Responses 2022-07-15 04_30 BST.xlsx
df = read_excel(inputFilepath, sheet="Sheet1", skip = 8)
# Step 1: Remove Duplicates
dfDuplicatesRemoved = df %>%
group_by(!!sym("Employee ID"), !!sym("Question Body")) %>%
filter(row_number() == n()) %>% # keep last row
ungroup()
# Step 2: Pivot Data
dfTransformed = dfDuplicatesRemoved %>%
select(!!sym("Employee ID"), !!sym("Question Body"), !!sym("Questionnaire Answer")) %>%
pivot_wider(id_cols="Employee ID", names_from="Question Body", values_from="Questionnaire Answer")
# Step 3: Obtain an Employee Profile by fetching all data except for question related data.
dfEmployeeProfile = df %>%
dplyr::select(-c("Question Body", "Questionnaire Answer", "Question", "Questionnaire")) %>%
group_by("Employee ID") %>%
filter(row_number() == n()) %>% # keep last row
ungroup()
# Step 4: Join transformed data with Employee Profile to have a complete view
dfTranformedWithEmployeeProfile = dfTransformed %>% right_join(dfEmployeeProfile)
write.xlsx(dfTranformedWithEmployeeProfile, outputFilepath)
rm(list = ls())
sort(names(df))
I've made a small dummy version of the survey data I'm trying to transform:
Exit Interview Responses | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Questionnaire Target Contexts | ||||||||||||||||||||||
Questionnaire Targets | ||||||||||||||||||||||
Supervisory Organization | ----------------------- | |||||||||||||||||||||
Include Subordinate Organizations | Yes | |||||||||||||||||||||
Created On or After | 01/06/2022 | |||||||||||||||||||||
Created On or Before | 30/06/2022 | |||||||||||||||||||||
Questionnaire Answers | ||||||||||||||||||||||
Effective Date | Business Process Reason | Employee ID | Voluntary or Involuntary | Manager | Supervisory Organization | Management Level | Location | Country | FTE % | Service in Years | Worker Type | Supervisory Org - 1 From The Top | Supervisory Org - 2 From The Top | Supervisory Org - 3 From The Top | Supervisory Org - 4 From The Top | Supervisory Org - 5 From The Top | Supervisory Org - 6 From The Top | Questionnaire | Respondent | Question | Question Body | Questionnaire Answer |
22/06/2022 | Terminate Employee > Voluntary > Resignation - Career | 123456 | Voluntary | John Smith | Commercial | Level 8 | UK | United Kingdom | 100 | 11.13 | Employee | Adam Smith | Michael Smith | Terry Smith | Jane Smith | Ken Doe | Jane Doe | Exit Interview | Raj Patel | Exit Interview - Career | I knew how I could develop my career across the company | Disagree |
22/06/2022 | Terminate Employee > Voluntary > Resignation - Career | 789101 | Voluntary | John Smith | Commercial | Level 8 | UK | United Kingdom | 100 | 11.13 | Employee | Adam Smith | Michael Smith | Terry Smith | Jane Smith | Ken Doe | Jane Doe | Exit Interview | Raj Patel | Exit Interview - Centrica Achieve | I understand what the company wants to achieve over the next year | Disagree |
22/06/2022 | Terminate Employee > Voluntary > Resignation - Career | 121314 | Voluntary | John Smith | Commercial | Level 8 | UK | United Kingdom | 100 | 11.13 | Employee | Adam Smith | Michael Smith | Terry Smith | Jane Smith | Ken Doe | Jane Doe | Exit Interview | Raj Patel | Exit Interview - Change | Is there anything we could have done to change your mind? | Nothing at the moment |
What is causing my code to only read the first row of data?
my attempt at reproducing the data I need to transform:
``` r
tibble::tribble(
example_df <- ~Effective.Date, ~Employee.ID, ~Manager, ~Location, ~Questionnaire, ~Respondent, ~Question, ~Question.Body, ~Questionnaire.Answer,
"22/06/2022", 123456L, "John Smith", "UK", "Exit Interview", "Raj Patel", "Exit Interview - Career", "I knew how I could develop my career across the company", "Disagree",
"22/06/2022", 789101L, "John Smith", "UK", "Exit Interview", "Raj Patel", "Exit Interview - Centrica Achieve", "I understand what the company wants to achieve over the next year", "Disagree",
"22/06/2022", 121314L, "John Smith", "UK", "Exit Interview", "Raj Patel", "Exit Interview - Change", "Is there anything we could have done to change your mind?", "Nothing at the moment"
)
#> # A tibble: 3 × 9
#> Effective.Date Emplo…¹ Manager Locat…² Quest…³ Respo…⁴ Quest…⁵ Quest…⁶ Quest…⁷
#> <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 22/06/2022 123456 John S… UK Exit I… Raj Pa… Exit I… I knew… Disagr…
#> 2 22/06/2022 789101 John S… UK Exit I… Raj Pa… Exit I… I unde… Disagr…
#> 3 22/06/2022 121314 John S… UK Exit I… Raj Pa… Exit I… Is the… Nothin…
#> # … with abbreviated variable names ¹Employee.ID, ²Location, ³Questionnaire,
#> # ⁴Respondent, ⁵Question, ⁶Question.Body, ⁷Questionnaire.Answer
Created on 2022-08-04 by the reprex package (v2.0.1)