-4

This is probably a silly question with an easy answer, but I have data that looks like this:

Person  Date    value
A   1-Jan-17    12
A   2-Jan-17    13
A   3-Jan-17    65
B   2-Jan-17    34
B   3-Jan-17    76
B   4-Jan-17    98
C   3-Jan-17    34
C   4-Jan-17    45
C   1-Jan-17    10

and I want to make it look like this:

Date    Person A    Person B    Person C
1-Jan-17    12  0   10
2-Jan-17    13  34  0
3-Jan-17    65  76  34
4-Jan-17    0   98  45

So that I have a full time series for each person. I get that I will probably have to pad the data for my full data set.

Any help would be appreciated. I have tried a number of things, but have not gotten what I want (waaaaah)

Pete
  • 321
  • 4
  • 16
  • `"tried a number of things"` always consider adding your code, even if it doesn't work. And just searching your question title would have given you the link to one of the most frequently asked R questions. – zx8754 Jul 19 '18 at 13:15
  • 1
    The options I found did not provide me with the answer I needed. This answer turned out to be perfect for this particular problem and I am glad that I have it now. Hopefully, it will help someone else, too. – Pete Jul 19 '18 at 15:25

1 Answers1

2

Check out tidyr and spread

library(tidyr)
df %>% spread(Person,value, fill = 0)
#       Date  A  B  C
# 1 1-Jan-17 12  0 10
# 2 2-Jan-17 13 34  0
# 3 3-Jan-17 65 76 34
# 4 4-Jan-17  0 98 45
jasbner
  • 2,253
  • 12
  • 24