0

this is a question about how to switch R data frames. The sample data frame below(data) | id | date | purchase| |:---|:-----| -----:| | a | 2022-01-01 |1 | | a | 2022-01-02 |3 | | a | 2022-01-03 |5 | | b | 2022-01-01 |2 | | b | 2022-01-04 |4 | | c | 2022-01-05 |6 |

I would like to ask you how to separate it into two data frames in the following form. | id | date1 | date2 |date3| |:---- |:------:|:-----:| -----:| | a | 2022-01-01 |2022-01-02 |2022-01-03| | b | 2022-01-01 |2022-01-04 | | c | 2022-01-05 |

id date1 date2 date3
a 1 3 5
b 2 4
c 6

I tried using ‘tidyr’ package’s ‘pivot_long’ method, but I want to avoid creating each column that combines id and date.

a <- pivot_wider(data = data, names_from = id, values_from = date)
## also 
a1<- data %>% pivot_wider(names_from = id, values_from = date, names_prefix = "purchase_date")

Thank you advance

0 Answers0