0

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Nothing wrong with the example data. `df1 %>% spread(Test, Result)` gives the desired result – Jaap Feb 07 '17 at 20:07
  • Scott this works for me as well can you provide more context? – sgp667 Feb 07 '17 at 20:17
  • I'm going to make a guess here, but maybe you have duplicate results for some tests, or some tests were administered twice for a patient on a given day. Use this to diagnose(this uses `dplyr` package) `your.data.frame %>% group_by(Patient,OrderDate,Test) %>% summarise(Count=n()) %>% filter(Count >1)` If I'm wrong this will not return any results, if I'm right you should see duplicated test/results – sgp667 Feb 07 '17 at 20:22
  • I think the relevant context sgp667 is that I am new to R and you pretty much hit the nail on the head. Jaap example works just perfect and I need to go back and be more carefully. Thanks for your patience! – Scott Purvis Feb 07 '17 at 21:16
  • Also should add that you were right, I had duplicate records in my data frame – Scott Purvis Feb 08 '17 at 14:57

0 Answers0