0

I'm new with R and I have the following problem. Maybe it's a really easy question, but I don't know the terms to search for an answer.

My problem: I have several persons, each person is assigned a studynumber (SN). And each SN has one or more tests being performed, the test can have multiple results. My data is long at the moment, but I need it to be wide (one row for each SN).

For example: What I have:


  SN testnumbers result
1  1        1234      6
2  1        1234      9
3  2        4567      6
4  3        5678      9
5  3        8790      9

What I want:

  SN test1result1 test1result2 test2result1
1  1            6            6           NA
2  2            6           NA           NA
3  3            9           NA            9

So I need to renumber the testnumbers into test 1 etc for each SN, in order to use the spread function, I think. But I don't know how.

I did manage to renumber testnumber into a list of 1 till the last unique testnumber, but still the wide dataframe looks awful.

AvdH
  • 39
  • 1
  • 6
  • If I understand what you're trying to do, group by `SN`, then make a column of row numbers along each group? Several answers here: https://stackoverflow.com/q/12925063/5325862 – camille May 06 '19 at 19:12
  • Thank you for the suggestion! It already helps a lot! – AvdH May 07 '19 at 19:40

0 Answers0