R - 用dplyr重复

时间:2015-07-14 10:24:28

标签: r dplyr rep

为了改变我的长期紧凑型"将数据格式化为宽格式我需要使用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"))

1 个答案:

答案 0 :(得分:3)

尝试

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