gpt4 book ai didi

r - ifelse 在 R 中有两个条件数字和分类

转载 作者:行者123 更新时间:2023-12-04 00:51:37 26 4
gpt4 key购买 nike

我有以下数据集,其中一个子集是:

structure(list(First.Name = c(5006L, 5006L, 5006L, 5006L, 5006L, 
5006L, 5006L, 5006L, 5006L, 5006L, 5006L, 5006L), TimePoint = c(NA,
NA, NA, NA, NA, "PRE", NA, NA, NA, NA, NA, NA), Year_Day = c(125,
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136), Week_Year = c(18,
18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20), Session = c("Pre",
"Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post",
"Post", "Post")), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))

看起来像(用星号表示错误):

# A tibble: 12 x 5
First.Name TimePoint Year_Day Week_Year Session
<int> <chr> <dbl> <dbl> <chr>
1 5006 NA 125 18 Pre
2 5006 NA 126 18 Pre
3 5006 NA 127 19 Pre
4 5006 NA 128 19 Pre
5 5006 NA 129 19 Pre
6 5006 PRE 130 19 Pre
7 5006 NA 131 19 **Pre**
8 5006 NA 132 19 **Pre**
9 5006 NA 133 19 **Pre**
10 5006 NA 134 20 Post
11 5006 NA 135 20 Post
12 5006 NA 136 20 Post

如果 Week_Year 是主题数据的开始,我正在尝试为每个主题创建一个名为 Session 的新列,其中包含单词“Pre”直到(包括) TimePoint 列包含单词“PRE”,所有其他行应为“Post”

上述子集的理想输出应该是:

# A tibble: 12 x 5
First.Name TimePoint Year_Day Week_Year Session
<int> <chr> <dbl> <dbl> <chr>
1 5006 NA 125 18 Pre
2 5006 NA 126 18 Pre
3 5006 NA 127 19 Pre
4 5006 NA 128 19 Pre
5 5006 NA 129 19 Pre
6 5006 PRE 130 19 Pre
7 5006 NA 131 19 **Post**
8 5006 NA 132 19 **Post**
9 5006 NA 133 19 **Post**
10 5006 NA 134 20 Post
11 5006 NA 135 20 Post
12 5006 NA 136 20 Post

我正在尝试各种变体

df %>%
group_by(First.Name) %>%
mutate(Session = ifelse(TimePoint == "PRE" & Week_Year <= first(Week_Year) + 1, "Pre", "Post")) %>%
ungroup()

但它没有正确输出。感谢帮助。

最佳答案

您可以使用 dplyr 包中的 case_when 以及 lagfill 来完成此操作。

df <- structure(list(First.Name = c(5006L, 5006L, 5006L, 5006L, 5006L, 
5006L, 5006L, 5006L, 5006L, 5006L, 5006L, 5006L), TimePoint = c(NA,
NA, NA, NA, NA, "PRE", NA, NA, NA, NA, NA, NA), Year_Day = c(125,
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136), Week_Year = c(18,
18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20), Session = c("Pre",
"Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post",
"Post", "Post")), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))

library(dplyr)
library(tidyr)

df %>%
mutate(
Session = case_when(
TimePoint == "PRE" ~ "Pre",
lag(TimePoint) == "PRE" ~ "Post")
) %>%
fill(Session, .direction = "updown") %>%
ungroup()

#> # A tibble: 12 x 5
#> First.Name TimePoint Year_Day Week_Year Session
#> <int> <chr> <dbl> <dbl> <chr>
#> 1 5006 <NA> 125 18 Pre
#> 2 5006 <NA> 126 18 Pre
#> 3 5006 <NA> 127 19 Pre
#> 4 5006 <NA> 128 19 Pre
#> 5 5006 <NA> 129 19 Pre
#> 6 5006 PRE 130 19 Pre
#> 7 5006 <NA> 131 19 Post
#> 8 5006 <NA> 132 19 Post
#> 9 5006 <NA> 133 19 Post
#> 10 5006 <NA> 134 20 Post
#> 11 5006 <NA> 135 20 Post
#> 12 5006 <NA> 136 20 Post

reprex package 创建于 2021-02-01 (v0.3.0)

关于r - ifelse 在 R 中有两个条件数字和分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66001859/

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