用R创建属性和日期的表

时间:2019-10-15 18:03:52

标签: r

我有一张表,列出了购买了每种产品的客户以及购买日期。我有他们买的东西和日期。像这样:

Client | Item | Date 
___________________________
John   | CD   | 2019-09-09
John   | LP   | 2019-09-08
John   | DVD  | 2019-09-07
Mary   | CD   | 2019-09-07
Mary   | Book | 2019-09-06
Mary   | DVD  | 2019-09-05
Louis  | CD   | 2019-09-06
Louis  | LP   | 2019-09-06 



I am using R. I tried this structure, which is the summarize function: 

df %>%
  group_by(Date, Item) %>%                           
  mutate(id = paste0("new_col_", row_number())) %>%  
  ungroup() %>%                                      
  spread(id, Client) 

But it returned the same client in various line, and I do not want this. I need in one.



I expect something like this:


Client | Book        |    CD      |   DVD       | LP
_____________________________________________________________________
John   |  N/A        | 2019-09-09 | 2019-09-07  | 2019-09-08
Mary   |  2019-09-06 | 2019-09-07 | 2019-09-05  | N/A
Louis  |  N/A        | 2019-09-06 | N/A         | 2019-09-06

0 个答案:

没有答案