根据两个条件创建一个新列

时间:2019-07-17 23:37:14

标签: r tidyverse

我有以下数据集:

yDF = structure(list(Date = structure(c(1L,2L,3L,4L,5L,6L,7L,9L,9L,10L,11L), .Label = c("3/31/2018","4/1/2018", "4/2/2018", "4/3/2018", "4/4/2018", 
                                                                                        "4/5/2018", "4/6/2018", "4/8/2018", "4/8/2018", "4/9/2018","4/10/2018"), class = "factor"), 
                     Group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                         2L), .Label = c("Decrease", "Increase"), class = "factor"), 
                     ID = c(5002, 5002, 5002, 5002, 5002, 5002, 5002, 5002, 
                            5002, 5002, 5002), Week = c(13, 13, 14, 14, 14, 14, 14, 
                                                        14, 14, 15, 15)), row.names = 1:10, class = "data.frame")

应如下所示:


    Date        Group       ID      Week
1   3/31/2018   Increase    5002    13
2   4/1/2018    Increase    5002    13
3   4/2/2018    Increase    5002    14
4   4/3/2018    Increase    5002    14
5   4/4/2018    Increase    5002    14
6   4/5/2018    Increase    5002    14
7   4/6/2018    Increase    5002    14
8   4/8/2018    Increase    5002    14
9   4/8/2018    Increase    5002    14
10  4/9/2018    Increase    5002    15

我想添加一个称为“会话”的新列

  • 如果ID列和Week列是新列,或者如果它们的值相同且在原始值的2周内,则将行标记为“ pre”。
  • 如果同一ID(例如5002)的周号是“原始周+ 2周或更多”(例如第15周),则应将其标记为“发布”。

我曾经玩过if else函数,但是无法获得正确的输出。理想情况下,它将如下所示:

(请记住,我有100多个唯一的主题ID)

```
        Date    Group   ID          Week   Session
30 3/31/2018 Increase 5002           13    Pre
31  4/1/2018 Increase 5002           13    Pre
32  4/2/2018 Increase 5002           14    Pre
33  4/3/2018 Increase 5002           14    Pre
34  4/4/2018 Increase 5002           14    Pre
35  4/5/2018 Increase 5002           14    Pre
36  4/6/2018 Increase 5002           14    Pre
37  4/8/2018 Increase 5002           14    Pre
38  4/8/2018 Increase 5002           14    Pre
39  4/9/2018 Increase 5002           15    Post
40 4/10/2018 Increase 5002           15    Post
```

1 个答案:

答案 0 :(得分:0)

我们可以使用mutateifelsefirst来完成此任务。

library(tidyverse)

yDF2 <- yDF %>%
  group_by(ID) %>%
  mutate(Session = ifelse(Week <= first(Week) + 1, "Pre", "Post")) %>%
  ungroup()
yDF2
# # A tibble: 10 x 5
#    Date      Group       ID  Week Session
#    <chr>     <chr>    <int> <int> <chr>  
#  1 3/31/2018 Increase  5002    13 Pre    
#  2 4/1/2018  Increase  5002    13 Pre    
#  3 4/2/2018  Increase  5002    14 Pre    
#  4 4/3/2018  Increase  5002    14 Pre    
#  5 4/4/2018  Increase  5002    14 Pre    
#  6 4/5/2018  Increase  5002    14 Pre    
#  7 4/6/2018  Increase  5002    14 Pre    
#  8 4/8/2018  Increase  5002    14 Pre    
#  9 4/8/2018  Increase  5002    14 Pre    
# 10 4/9/2018  Increase  5002    15 Post  

数据

yDF <- read.table(text = "    Date        Group       ID      Week
1   '3/31/2018'   Increase    5002    13
2   '4/1/2018'    Increase    5002    13
3   '4/2/2018'    Increase    5002    14
4   '4/3/2018'    Increase    5002    14
5   '4/4/2018'    Increase    5002    14
6   '4/5/2018'    Increase    5002    14
7   '4/6/2018'    Increase    5002    14
8   '4/8/2018'    Increase    5002    14
9   '4/8/2018'    Increase    5002    14
10  '4/9/2018'    Increase    5002    15",
                  stringsAsFactors = FALSE, header = TRUE)
相关问题