I am confronted with messy data. A particular point where I'm stuck is that some values are hidden inside some variables, subsetting the table as some kind of "header".
An example:
df <- data.frame(
a = c("header1", "value", "value", "header2", "value", "value"),
b = c(1, 2, 3, 4, 5, 6)
)
What I roughly want:
df_goal <- data.frame(
a = c("header1", "value", "value", "header2", "value", "value"),
b = c(1, 2, 3, 4, 5, 6),
c = c("header1", "header1", "header1", "header2", "header2", "header2")
)
So it's basically about manipulating data based on the position of the "headers".
Edit
Answers so far revolve around the "headers" following some pattern, e.g. "header1", etc. In this case, they work great.
I would, however like to come up with a more general solution where "headers" are arbitrary, e.g. "fererfw" and "ewetwet" instead of "header1" and "header2" in the above example.