重复标识符的展开(使用tidyverse和%>%)

9

我的数据看起来像这样:

enter image description here

我正在尝试使它看起来像这样:

enter image description here

我希望使用%>%链接在tidyverse中完成这个操作。
df <- 
structure(list(id = c(2L, 2L, 4L, 5L, 5L, 5L, 5L), start_end = structure(c(2L, 
1L, 2L, 2L, 1L, 2L, 1L), .Label = c("end", "start"), class = "factor"), 
    date = structure(c(6L, 7L, 3L, 8L, 9L, 10L, 11L), .Label = c("1979-01-03", 
    "1979-06-21", "1979-07-18", "1989-09-12", "1991-01-04", "1994-05-01", 
    "1996-11-04", "2005-02-01", "2009-09-17", "2010-10-01", "2012-10-06"
    ), class = "factor")), .Names = c("id", "start_end", "date"
), row.names = c(3L, 4L, 7L, 8L, 9L, 10L, 11L), class = "data.frame")

我尝试过的:

data.table::dcast( df, formula = id ~ start_end, value.var = "date", drop = FALSE )  # does not work because it summarises the data

tidyr::spread( df, start_end, date )  # does not work because of duplicate values


df$id2 <- 1:nrow(df)
tidyr::spread( df, start_end, date ) # does not work because the dataset now has too many rows.

这些问题并不能回答我的问题: 使用spread处理具有重复标识符的行(因为它们只是总结) R中处理具有重复数据框的spread函数(因为它们将值粘贴在一起) 使用“登录”“注销”时间在R中重塑数据(因为不是特别要求/使用tidyverse和链接来回答)

as.data.table(df)[, .id := sequence(.N), .(id, start_end)][, dcast(.SD, .id + id ~ start_end, value.var = "date")] - A5C1D2H2I1M1N2O1R2T1
使用 reshape2dplyrdf %>% group_by(id, start_end) %>% arrange(date) %>% mutate(sequence=1:n()) %>% dcast(id + sequence ~ start_end, value="date") - eipi10
1个回答

16

我们可以使用tidyverse。按照 'start_end'、'id' 进行分组后,创建一个序列列 'ind',然后将数据从长格式 spread 到宽格式。

library(dplyr)
library(tidyr)
df %>%
   group_by(start_end, id) %>%
   mutate(ind = row_number()) %>%
   spread(start_end, date) %>% 
   select(start, end)
#     id      start        end
#* <int>     <fctr>     <fctr>
#1     2 1994-05-01 1996-11-04
#2     4 1979-07-18         NA
#3     5 2005-02-01 2009-09-17
#4     5 2010-10-01 2012-10-06

或者使用tidyr_1.0.0

chop(df, date) %>%
     spread(start_end, date) %>%
     unnest(c(start, end))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接