Creating multiple new rows with certain columns in R

  Kiến thức lập trình

I have a dataset that includes multiple biological samples collected for each patient, but within the data output all samples are in the same row. I would like to manipulate the data so that each sample is in its own row.

The dataframe currently looks like the following: (this is a simplified fake version of the data)

Patient_ID   Patient_name   Sample_1_ID   Sample_1_value1  Sample_1_value2   Sample_2_ID   Sample_2_value1   Sample_2_value2  Sample_3_ID   Sample_3_value1   Sample_3_value2
AB100         AB             CSF            1.0             2.0               CSF            2.0               3.0             CSF              2.0             4.0
JM200         JM             CSF            2.0             3.0               CSF            4.0               5.0             CSF              3.0             4.0

I would like the final dataframe to look like:

Patient_ID   Patient_name   Sample_ID   Sample_value1  Sample_value2
AB100          AB             CSF            1.0             2.0
AB100          AB             CSF            2.0             3.0
AB100          AB             CSF            2.0             4.0
JM200          JM             CSF            2.0             3.0
JM200          JM             CSF            4.0             5.0
JM200          JM             CSF            3.0             4.0

I tried to do this using dplyr’s add_row() function, but I can’t figure out how to duplicate the patient identifier rows while moving down just the sample data.

Thank you in advance.

LEAVE A COMMENT