My data are in the following wide format, in rows according to SUBJECT_ID
, with a total of observations of variables X
and Y
and then various columns of metadata e.g. SUBJECT_BIRTHYEAR
, SUBJECT_HOMETOWN
:
variableX variableY SUBJECT_ID SUBJECT_BIRTHYEAR SUBJECT_HOMETOWN
2 1 A 1950 Townsville
1 2 B 1951 Villestown
I would like to transfom these into the following long format, where for each observation of variable X
and Y
for each SUBJECT_ID
:
VARIABLE SUBJECT_ID SUBJECT_BIRTHYEAR SUBJECT_HOMETOWN
X A 1950 Townsville
X A 1950 Townsville
Y A 1950 Townsville
X B 1951 Villestown
Y B 1951 Villestown
Y B 1951 Villestown
Specific to my question is how to transform n observations of a continuous variable into n rows of categorical data.