rdplyrrep

R - repetition with dplyr


To transform my "long compact" format data into wide format, I need to use the rep function.

I cannot figure out how to integrate it into a dplyr flow.

This is the repetition I need to use:

dta1 = as.data.frame(cbind(rep(dta$id, dta$duration), rep(dta$act, dta$duration) ) ) 
colnames(dta1) <- c('id', 'act')

Here is the dplyr code.

dta1 %>%
group_by(id) %>% 
mutate(Time = 1:n() ) %>%
spread(Time, act)

Do you have any idea how could I put these two codes together?

The data

dta = structure(list(id = c("B10001N1", "B10001N1", "B10001N1", "B10001N1", 
                  "B10001N1", "B10001N1", "B10001N1", "B10001N1", "B10001N1", "B10001N1", 
                  "B10001N1", "B10001N1", "B10001N1", "B10001N1", "B10001N1", "B10001N1", 
                  "B10001N2", "B10001N2", "B10001N2", "B10001N2", "B10001N2", "B10001N2", 
                  "B10001N2", "B10001N2", "B10001N2", "B10001N2", "B10001N2", "B10001N2", 
                  "B10001N2", "B10001N3", "B10001N3", "B10001N3", "B10001N3", "B10001N3", 
                  "B10001N3", "B10001N3", "B10001N3", "B10001N3", "B10001N3", "B10001N3", 
                  "B10001N3", "B10001N3", "B10001N4", "B10001N4", "B10001N4", "B10001N4", 
                  "B10001N4", "B10001N4", "B10001N4", "B10001N4", "B10001N4", "B10001N4", 
                  "B10001N4", "B10001N4", "B10001N4"), act = c("-11", "1704", "1302", 
                                                               "1301", "1507", "603", "1301", "101", "502", "1704", "1507", 
                                                               "1404", "8888", "603", "1507", "101", "-11", "1302", "1301", 
                                                               "1507", "704", "101", "1704", "1704", "3102", "1002", "1704", 
                                                               "3101", "101", "-11", "1704", "1302", "1302", "1507", "603", 
                                                               "2902", "3201", "812", "1704", "1704", "3701", "101", "-11", 
                                                               "1302", "1301", "3101", "1001", "1507", "1006", "2101", "2902", 
                                                               "1704", "8888", "1704", "1302"), duration = c(30, 570, 5, 30, 
                                                                                                             25, 3, 12, 165, 30, 10, 5, 20, 70, 45, 180, 240, 570, 30, 30, 
                                                                                                             20, 25, 95, 70, 20, 20, 20, 60, 45, 435, 30, 30, 570, 90, 30, 
                                                                                                             15, 5, 40, 60, 240, 60, 30, 240, 600, 15, 45, 15, 75, 30, 150, 
                                                                                                             60, 30, 60, 210, 60, 90)), row.names = c(NA, 55L), class = "data.frame", .Names = c("id", 
                                                                                                                                                                                                 "act", "duration"))

Solution

  • Try

    library(dplyr)
    library(tidyr)
    dta[rep(1:nrow(dta), dta$duration), -3] %>%
      group_by(id) %>% 
      mutate( Time = 1:n() ) %>%
      spread(Time, act)