R - 用 dplyr 重复

R - repetition with dplyr

要将我的“长压缩”格式数据转换为宽格式,我需要使用 rep 函数。

我不知道如何将它集成到 dplyr 流程中。

这是我需要使用的重复:

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

这是 dplyr 代码。

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

你知道我怎样才能把这两个代码放在一起吗?

数据

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"))

尝试

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