I am trying to use rep
with dplyr
but I do not fully understand why I can not make it work.
My data look like this. What I want is to simply repeat dayweek
by n
for each id
.
head(dt4)
id dayweek n
1 1 Friday 3
2 1 Monday 3
3 1 Saturday 3
4 1 Sunday 3
5 1 Thursday 3
6 1 Tuesday 3
What I am trying to do is this within a dplyr
flow
cbind(rep(dt4$id, dt4$n), rep(as.character(dt4$dayweek), dt4$n) )
which gives
[,1] [,2]
[1,] "1" "Friday"
[2,] "1" "Friday"
[3,] "1" "Friday"
[4,] "1" "Monday"
[5,] "1" "Monday"
[6,] "1" "Monday"
I do not understand why this code does not work
dt4 %>%
group_by(id) %>%
summarise(rep(dayweek, n))
Error: expecting a single value
Could someone help me with this ?
the data
dt4 = structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), dayweek = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L), .Label = c("Friday", "Monday", "Saturday", "Sunday",
"Thursday", "Tuesday", "Wedesnday"), class = "factor"), n = c(3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), class = "data.frame", .Names = c("id",
"dayweek", "n"), row.names = c(NA, -21L))