Hi,
I have received a df which has incorrect structure and needs transposing:
source <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
UserNo = c(5, 6, 7, 9, 14, 16),
Started = c("2021-11-08 08:53:00","2021-11-08 13:26:00",
"2021-11-08 23:59:00","2021-11-09 10:11:00",
"2021-11-10 15:31:00","2021-11-15 09:25:00"),
`Q2. Function` = c("NSC",
"XYZ","NSC","Customer Excellence","XYZ",
"NSC"),
`Q4. Market` = c("-", "adssad", "-", "-", "fghjfj", "-"),
`Q5. Team` = c("-", "-", "-", "bbb", "-", "-"),
`Q6. How often` = c("Dayly", "Weekly", "-", "-", "Weekly", "-"),
`Q7. How would you rate` = c("9", "10", "-", "-", "7", "-"),
`Q21. How could we improve` = c("sdfgh s", "-", "-", "-", "-", "-"),
`Q22. Team` = c("-", "-", "fff", "-", "-", "-"),
`Q23. How often` = c("-", "-", "Monthly", "-", "-", "-"),
`Q24. How would you rate` = c("-", "-", "9", "-", "-", "-"),
`Q38. How could we improve` = c("-", "-", "fgsd", "-", "-", "-"),
`Q73. Team` = c("-", "-", "-", "-", "-", "aaa"),
`Q74. How often` = c("-","-",
"-","-","-",
"Fortnightly"),
`Q75. How would you rate` = c("-", "-", "-", "-", "-", "9"),
`Q89. How could we improve` = c("-", "-", "-", "-", "-", "xxx")
)
Q22 and Q73 are the same as Q5, Q23 and Q74 are the same as Q6, Q24 and Q75 are the same as Q7, Q38 and Q89 are the same as Q21 so they should be transposed and the result should look like that.
result <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
UserNo = c(5,6,7,9,14,16,
5,6,7,9,14,16,5,6,7,9,14,16),
Started = c("2021-11-08 08:53:00","2021-11-08 13:26:00","2021-11-08 23:59:00",
"2021-11-09 10:11:00","2021-11-10 15:31:00",
"2021-11-15 09:25:00","2021-11-08 08:53:00",
"2021-11-08 13:26:00","2021-11-08 23:59:00",
"2021-11-09 10:11:00","2021-11-10 15:31:00","2021-11-15 09:25:00",
"2021-11-08 08:53:00","2021-11-08 13:26:00",
"2021-11-08 23:59:00","2021-11-09 10:11:00",
"2021-11-10 15:31:00","2021-11-15 09:25:00"),
`Q2. Function` = c("NSC","XYZ","NSC",
"Customer Excellence","XYZ","NSC","NSC","XYZ",
"NSC","Customer Excellence","XYZ","NSC","NSC",
"XYZ","NSC","Customer Excellence","XYZ","NSC"),
`Q4. Market` = c("-","adssad","-",
"-","fghjfj","-","-","adssad","-","-",
"fghjfj","-","-","adssad","-","-","fghjfj","-"),
Team = c("-","-","-",
"bbb","-","-","-","-","fff","-","-","-","-",
"-","-","-","-","aaa"),
`How often` = c("Dayly","Weekly",
"-","-","Weekly","-","-","-","Monthly","-",
"-","-","-","-","-","-","-","Fortnightly"),
`How would you rate` = c("9","10","-","-",
"7","-","-","-","9","-","-","-","-","-",
"-","-","-","9"),
`How could we improve` = c("sdfgh s","-","-",
"-","-","-","-","-","fgsd","-","-","-",
"-","-","-","-","-","xxx")
)
Is any clever way of transposing two sets of four questions below the first set?
In my real data I have more than these two sets in the example above...