I have survey data where one respondent per household was asked about the age and education level of each member of the household. The data are wide, in the sense that there are indexed columns for the age and education of each household member.
Here's a simple example:
df <-
data.frame(
HHID = 1:2,
age_1 = c(4, 19),
yrsedu_1 = c(8,12),
age_2 = c(7, 6),
yrsedu_2 = c(14, 6)
)
Thus age_1 is the age of one member of household 1 and edu_2 is their education level.
Essentially I want to stack the pairs of columns (2 columns per household member) to end up with a long dataset like this:
data.frame(
HHID = c(1,1,2,2),
hh_child_number = c(1,2,1,2),
age = c(4,7,19,6),
yrsedu = c(8,14,12,6))
How can I do this with gather in tidyr? The pairs of columns are causing trouble.