gpt4 book ai didi

r - 根据列的第一个值对所有行进行子集

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

我有一个子集问题,我被卡住了。这是数据的一个片段:

UniqueID MonthYear FirstObs
ABC123 OCT-18 1
ABC123 NOV-18 0
ABC123 JAN-19 0
ABC123 FEB-19 0
DEF446 MAY-19 1
DEF456 JUN-19 0
DEF456 JUL-19 0
GHI789 OCT-18 1
GHI789 NOV-18 0

数据集非常大,并且有一系列类似于上述示例的行。我希望能够编写一个子集公式,该公式提取具有相同的 UniqueID 块的每一行,这些块以 FirstObs=1 开头,并根据它们起源的月份将它们放在一起。我会有如下内容:
Subset1 (all uniqueIDs that originated in October)
UniqueID MonthYear FirstObs
ABC123 OCT-18 1
ABC123 NOV-18 0
ABC123 JAN-19 0
ABC123 FEB-19 0
GHI789 OCT-18 1
GHI789 NOV-18 0


Subset2 (all uniqueIDs that originated in May)
UniqueID MonthYear FirstObs
DEF446 MAY-19 1
DEF456 JUN-19 0
DEF456 JUL-19 0

理想情况下,我将为每个月以 FirstObs=1 开头的每个块设置一个子集。我知道我需要使用一系列 ifelse 和子集函数,但我不确定如何以最佳方式使用它们。

最佳答案

可能这有帮助

library(dplyr)
df2 <- df1 %>%
group_by(UniqueID) %>%
filter(first(FirstObs) == 1 & n() > 1)
df3 <- anti_join(df1, df2)

也可以
library(stringr)
df2 <- df1 %>%
group_by(UniqueID) %>%
filter(first(FirstObs) == 1,
str_remove(first(MonthYear), "-\\d+") == "OCT")
df3 <- anti_join(df1, df2)

或者 split 变成 data.frames 的 list
df1 %>%
group_by(UniqueID) %>%
mutate(grp = first(FirstObs) == 1 & n() > 1) %>%
ungroup %>%
group_split(grp, keep = FALSE)
#[[1]]
# A tibble: 3 x 3
# UniqueID MonthYear FirstObs
# <chr> <chr> <int>
#1 DEF446 MAY-19 1
#2 DEF456 JUN-19 0
#3 DEF456 JUL-19 0

#[[2]]
# A tibble: 6 x 3
# UniqueID MonthYear FirstObs
# <chr> <chr> <int>
#1 ABC123 OCT-18 1
#2 ABC123 NOV-18 0
#3 ABC123 JAN-19 0
#4 ABC123 FEB-19 0
#5 GHI789 OCT-18 1
#6 GHI789 NOV-18 0

数据
df1 <- structure(list(UniqueID = c("ABC123", "ABC123", "ABC123", "ABC123", 
"DEF446", "DEF456", "DEF456", "GHI789", "GHI789"), MonthYear = c("OCT-18",
"NOV-18", "JAN-19", "FEB-19", "MAY-19", "JUN-19", "JUL-19", "OCT-18",
"NOV-18"), FirstObs = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L)),
class = "data.frame", row.names = c(NA,
-9L))

关于r - 根据列的第一个值对所有行进行子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670261/

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