gpt4 book ai didi

r - pivot_longer 对于具有相同 names_to 的多个集合

转载 作者:行者123 更新时间:2023-12-05 09:30:15 28 4
gpt4 key购买 nike

我正在尝试使用多个变量集的 pivot_longer,但我无法从示例中正确获取语法。

我的虚拟数据集是:

library(dplyr)
library(tidyr)

ID = c("id-1", "id-2", "id-3")
State = c("MD", "MD", "VA")
Time1Day= c( 1, 12, 30)
Time1Month = c( 1, 4, 5)
Time2Day = c( 9, 21, 13)
Time2Month = c( 12, 4, 5)
Time3Day = c( 7, 14, NA)
Time3Month = c( 1, 2, NA)


df <-data.frame(ID, State, Time1Day, Time1Month, Time2Day, Time2Month, Time3Day, Time3Month)

我想要的结果是:

    ID State  Time Day Month
1 id-1 MD Time1 1 1
2 id-1 MD Time2 9 12
3 id-1 MD Time3 7 1
4 id-2 MD Time1 12 4
5 id-2 MD Time2 21 4
6 id-2 MD Time3 14 2
7 id-3 VA Time1 30 5
8 id-3 VA Time2 13 5

我看过herehere尝试获得正确的语法,并尝试了以下两种解决方案,但我无法开始工作:

df.long <- df %>% 
pivot_longer(cols = starts_with("Time"), names_to = c("Day", "Month"), names_sep="(?=[0-9])"), values_to = "Time", values_drop_na = TRUE)

df.long <- df %>%
pivot_longer(cols = ends_with("Day"), names_to = c("Time"), values_to = "Days", values_drop_na = TRUE) %>%
pivot_longer(cols = ends_with("Month"), names_to = c("Time"), values_to = "Months", values_drop_na = TRUE)

任何关于我缺少什么以及如何解决它的建议将不胜感激

最佳答案

编辑 由于 TarJae 的评论,添加了 values_drop_na = TRUE

你可以使用

library(dplyr)
library(tidyr)

df %>%
pivot_longer(-c(ID, State),
names_to = c("Time", ".value"),
names_pattern = "(Time\\d)(.*)",
values_drop_na = TRUE)

返回

# A tibble: 9 x 5
ID State Time Day Month
<chr> <chr> <chr> <dbl> <dbl>
1 id-1 MD Time1 1 1
2 id-1 MD Time2 9 12
3 id-1 MD Time3 7 1
4 id-2 MD Time1 12 4
5 id-2 MD Time2 21 4
6 id-2 MD Time3 14 2
7 id-3 VA Time1 30 5
8 id-3 VA Time2 13 5

关于r - pivot_longer 对于具有相同 names_to 的多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69798752/

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