I am trying to merge two longitudinal data which are both in the long format.
df1:
patientid visit mental-health
703-FD 1 depressed
703-FD 2 depressed
703-FD 3 depressed
707-NM 1 non-depressed
707-NM 2 non-depressed
707-NM 3 depressed
df2:
patientid visit HIV_disclosure
703-FD 1 yes
703-FD 2 yes
703-FD 3 yes
707-NM 1 no
707-NM 2 no
707-NM 3 yes
Code I've tried:
data_combined <- full_join(x=df1, y=df2, by="patientid"):
patientid visit.x mental-health visit.y HIV disclosure
703-FD 1 depressed 1 yes
703-FD 1 depressed 2 yes
703-FD 1 depressed 3 yes
703-FD 2 depressed 1 yes
703-FD 2 depressed 2 yes
703-FD 2 depressed 3 yes
703-FD 3 depressed 1 yes
703-FD 3 depressed 2 yes
703-FD 3 depressed 3 yes
707-NM 1 non-depressed 1 no
707-NM 1 non-depressed 2 no
707-NM 1 non-depressed 3 yes
707-NM 2 non-depressed 1 no
707-NM 2 non-depressed 2 no
707-NM 2 non-depressed 3 yes
707-NM 3 depressed 1 no
707-NM 3 depressed 2 no
707-NM 3 depressed 3 yes
How do I edit the above code to merge by both the patientid and the visit variable?
I've tried:
library (dplyr)
data_combined <- full_join(x=df1, y=df2, by="patientid", "visit")
Desired joined/merged dataframe:
patientid visit mental-health HIV disclosure
703-FD 1 depressed yes
703-FD 2 depressed yes
703-FD 3 depressed yes
707-NM 1 non-depressed no
707-NM 2 non-depressed no
707-NM 3 depressed yes
I'm sure it's a simple code, but I've been struggling with it for a while; please assist.