I have a wide df that I would like to convert to long form based on multiple columns. For example, all columns with "Type" would be in a single column and all columns with "Finding" would be in a single column. I believe dplyr
is the best approach but have not had luck with pivot_longer
.
Initial df
structure(list(Date = structure(c(1648512000, 1648598400, 1648166400, 1648166400),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Site = c("A", "B", "C", "D"),
`Finding?` = c("Yes", "Yes", "Yes", "Yes"),
`Topic Area` = c("A", "B", "C", "D"),
`Type` = c("1", "2", "3", "4"),
`Risk Ranking` = c("Medium", "Low", "Medium", "Medium"),
`Additional Finding?` = c("Yes", "Yes", "Yes", "Yes"),
`Topic Area2` = c("A", "B", "C", "D"),
`Type2` = c("1", "2", "2", "3"),
`Risk Ranking2` = c("Medium", "Medium", "Low", "Medium")),
row.names = c(NA, -4L),
class = c("tbl_df", "tbl", "data.frame"))
Desired output
data.frame(Date = structure(c(1648512000, 1648598400, 1648166400, 1648166400, 1648512000, 1648598400, 1648166400, 1648166400),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Site = c("A", "B", "C", "D", "A", "B", "C", "D"),
"Finding?" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
"Topic Area" = c("A", "B", "C", "D", "A", "B", "C", "D"),
`Type` = c("1", "2", "3", "4", "1", "2", "2", "3"),
"Risk Ranking" = c("Medium", "Low", "Medium", "Medium", "Medium", "Medium", "Low", "Medium"))