I have been working with a data set something like this:
df <- tribble(
~id, ~price, ~day,
"1", 10, '3',
"1", 5, '1',
"2", 7, '4',
"2", 6, '2',
"2", 3, '4',
"3", 4, '1',
"4", 5, '1',
"4", 6, '1',
"5", 1, '2',
"5", 9, '3',
)
However, the real data has almost 50 unique values in the day. For the analysis, I would like to see the median prices for each day and for each id. Here is the desired data (the values are not correct):
df <- tribble(
~id, ~day_1, ~day_2, ~day_3, ~day_4,
"1", 1, 1, 1, 1,
"2", 1, 1, 1, 1,
"3", 1, 1, 1, 1,
"4", 1, 1, 1, 1,
"5", 1, 1, 1, 1,
)
To do this, I've tried to code it as I've shown below. But, I was not able to cut day variable since it is the string variable.
df %>%
mutate(date_day = cut(day)) %>%
select(-day) %>%
pivot_wider(names_from = date_day, values_from = median(price)) %>%
adorn_percentages()
What is the way to do it? Thanks!