I have this data frame
Patient OrderDate Test Result
Patient1 1/1/2016 Test1 1
Patient1 1/1/2016 Test2 2
Patient2 2/1/2016 Test1 3
Patient2 2/1/2016 Test2 4
Patient1 3/1/2016 Test1 5
Patient1 3/1/2016 Test2 6
Patient2 4/1/2016 Test1 7
Patient2 4/1/2016 Test2 8
And I want to get to this data frame
Patient OrderDate Test1 Test2
Patient1 1/1/2016 1 2
Patient1 3/1/2016 5 6
Patient2 2/1/2016 3 4
Patient2 4/1/2016 7 8
It is clear to me that I need to use both spread()
and some kind of grouping function that works on 2 variables (in this case Name and Date), but I am lost.
I started with spread()
spread(df1, Test, Result)
Recived this Error:
Duplicate identifiers for rows (1, 5), (3, 7), (2, 6), (4, 8)
I have found folks use dcast()
, but this always seem to include and aggregate function that I am not interested in. I also don't want to unite the name or date into one variable column.
My ultimate goal is to generate a scatter plot for each Name/Date observation.