Following @Ronak Shah's suggestion, I'm pasting my code here in case my column names are less structured. I'm building up this code on the dat
data I specified in my initial post.
names(dat) <- c("id", "Q4", "Q5", "Q8r1", "Q8r2")
spec <- data.frame(.name = names(dat)[-1],
.value = c("Q4Q5", "Q4Q5", "Q8", "Q8"),
brand = rep(c("pepsi", "cola"), 2),
stringsAsFactors = FALSE)
dat_long <- pivot_longer_spec(dat, spec)
This gives essentially the same result as for the data with my structured names, just that the names are different now.
Again, I'm not entirely sure how generalizable this approach is, but it worked in my case.