I have a dataset that looks like this:
input <-
data.frame(
event = 1:2,
url_1 = c("g1", "g3"),
name_1 = c("dc", "nyc"),
url_2 = c("g2", "g4"),
name_2 = c("sf", "la"))
Essentially there are pairs of indexed columns that are stuck together in wide form. I want to convert to long to give this output:
output <-
data.frame(
event = c(1,1,2,2),
url = c("g1", "g2", "g3", "g4"),
name = c("dc", "sf", "nyc", "la"))
I want to do this using pivot_longer
. I've tried this:
input %>%
pivot_longer(contains("_"))
How can I get the function to recognize the column-pairs?