gpt4 book ai didi

r - 使用重复标识符传播(使用 tidyverse 和 %>%)

转载 作者:行者123 更新时间:2023-12-04 03:10:45 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Reshaping data in R with "login" "logout" times

(6 个回答)


5年前关闭。




我的数据如下所示:

enter image description here

我试图让它看起来像这样:

enter image description here

我想在 tidyverse 中使用 %>%-chaining 来做到这一点。

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.

这些问题不回答我的问题:

Using spread with duplicate identifiers for rows (因为他们总结)

R: spread function on data frame with duplicates (因为他们将值粘贴在一起)

Reshaping data in R with "login" "logout" times (因为没有使用 tidyverse 和链接专门询问/回答)

最佳答案

我们可以使用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))

关于r - 使用重复标识符传播(使用 tidyverse 和 %>%),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com