I am trying to convert a long dataframe to a wide dataframe using the spread function in tidyr and within a dplyr pipe. My data is in the following format:-
df<-structure(list(Hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
dt = c("05/06/2021", "07/06/2021", "08/06/2021", "10/06/2021",
"12/06/2021", "12/06/2021", "14/06/2021", "14/06/2021", "15/06/2021",
"09/06/2021", "10/06/2021", "10/06/2021", "11/06/2021", "13/06/2021",
"14/06/2021", "05/06/2021", "06/06/2021", "07/06/2021", "08/06/2021",
"08/06/2021", "09/06/2021", "09/06/2021", "10/06/2021", "11/06/2021",
"12/06/2021"), Programme_watched = c("Wrestling", "Drama",
"Drama", "Football", "Rugby", "Movie", "Movie", "News", "Music",
"Tennis", "Racing", "Racing", "Documentary ", "Movie", "Movie",
"Athletics", "News", "Children", "Music", "Movie", "Football",
"News", "Football", "Rugby", "Drama")), class = "data.frame", row.names = c(NA,
-25L))
df
Hour dt Programme_watched
1 0 05/06/2021 Wrestling
2 0 07/06/2021 Drama
3 0 08/06/2021 Drama
4 0 10/06/2021 Football
5 0 12/06/2021 Rugby
6 0 12/06/2021 Movie
7 0 14/06/2021 Movie
8 0 14/06/2021 News
9 0 15/06/2021 Music
10 1 09/06/2021 Tennis
11 1 10/06/2021 Racing
12 1 10/06/2021 Racing
13 1 11/06/2021 Documentary
14 1 13/06/2021 Movie
15 1 14/06/2021 Movie
16 2 05/06/2021 Athletics
17 2 06/06/2021 News
18 2 07/06/2021 Children
19 2 08/06/2021 Music
20 2 08/06/2021 Movie
21 2 09/06/2021 Football
22 2 09/06/2021 News
23 2 10/06/2021 Football
24 2 11/06/2021 Rugby
25 2 12/06/2021 Drama
I want to be able to make this wider, turning the dates into the column headings with the Programme_watched variable as the value. However, this is what I get:-
df%>%
group_by(Hour,dt)%>%
summarise(Programmes=Programme_watched)%>%
spread(dt, Programmes)
`summarise()` has grouped output by 'Hour', 'dt'. You can override using the `.groups` argument.
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 10 rows:
* 19, 20
* 21, 22
* 11, 12
* 5, 6
* 7, 8
Is it because there is more than one value for some of the grouping variables i.e. Hour and dt ? If so, I would like these two values to be pasted into the one cell together, then spread by the dt variable. Can anyone help?