【问题标题】:How to get x days after certain condition in R如何在R中的某些条件后获得x天
【发布时间】:2021-02-15 09:11:39
【问题描述】:

我有一个包含多个日期和条件的数据集。我想提取所有以条件 place == "A" 开头的行和所有以 place == "A" 开始日期和最多 7 天后的行。例如:

Date        Place       Value1      Value2
2018-10-27  C           20          8 
2018-10-29  A           10          5
2018-10-31  B           15          6
2018-11-4   C           17          9 
2018-11-8   D           18          5  

我想要:

Date        Place       Value1      Value2
2018-10-29  A           10          5
2018-10-31  B           15          6
2018-11-4   C           17          9 

如您所见,它必须在 7 天内提取具有 place == A 的第一行和所有行。第一天之后的地方像“A”没有意义,像“B”和“C”。它必须以“A”开头。它会跳过 2018-11-8,因为距离 2018-10-29 超过 7 天。

我试过这样的问题:R: Extract data based on date, "if date lesser than",但我不知道如何提取 7 天。

【问题讨论】:

    标签: r date extract


    【解决方案1】:

    我们可以使用match获取对应的Date值,并从中选择7天内的所有行。

    library(dplyr)
    
    df %>%
      mutate(Date = as.Date(Date)) %>%
      filter({tmp <- Date[match('A', Place)] 
              between(Date, tmp, tmp + 7)})
    
    #        Date Place Value Value.1
    #1 2018-10-29     A    10       5
    #2 2018-10-31     B    15       6
    #3 2018-11-04     C    17       9
    

    dplyr 允许在全局环境中不创建临时变量的情况下执行操作,上面的解决方案可以用base R写成:

    df$Date <- as.Date(df$Date)
    date_val <- df$Date[match('A', df$Place)]
    subset(df, Date >= date_val & Date <= date_val + 7)
    

    数据

    df <- structure(list(Date = structure(c(17831, 17833, 17835, 17839, 
    17843), class = "Date"), Place = c("C", "A", "B", "C", "D"), 
        Value = c(20L, 10L, 15L, 17L, 18L), Value.1 = c(8L, 5L, 6L, 
        9L, 5L)), row.names = c(NA, -5L), class = "data.frame")
    

    【讨论】:

      【解决方案2】:

      Base R 中的一个选项是

      # Find the difference in days 
      
      tmp1 <- df$Date - df[df$Place == "A", "Date"]
      
      # Time differences in days
      # [1] -2  0  2  6 10
      
      # And then just subset your df 
      
      df[df$Place == "A" | (tmp1  <= 7 & tmp1 > 0), ]
      
      #         Date Place Value Value.1
      # 2 2018-10-29     A    10       5
      # 3 2018-10-31     B    15       6
      # 4 2018-11-04     C    17       9
      

      数据

      df <- read.table( text = "Date        Place       Value       Value
      2018-10-27  C           20          8 
      2018-10-29  A           10          5
      2018-10-31  B           15          6
      2018-11-4   C           17          9 
      2018-11-8   D           18          5 ", header = T)
      
      df[, 1] <- as.Date(df[, 1])
      

      【讨论】:

        【解决方案3】:

        即使这与 Ronak 的答案几乎相似,但也可以工作,但无需创建 tmp 变量。

        #dput
        dat <- structure(list(Date = c("2018-10-27", "2018-10-29", "2018-10-31", 
        "2018-11-04", "2018-11-08"), Place = c("C", "A", "B", "C", "D"
        ), Value1 = c(20L, 10L, 15L, 17L, 18L), Value2 = c(8, 5, 6, 9, 
        5)), class = "data.frame", row.names = c(NA, -5L))
        
        #code
        library(dplyr)
        dat %>% mutate(Date = as.Date(Date)) %>%
          filter(between(Date, Date[Place == "A"], Date[Place == "A"] + 7))
                Date Place Value1 Value2
        1 2018-10-29     A     10      5
        2 2018-10-31     B     15      6
        3 2018-11-04     C     17      9
        

        【讨论】:

          猜你喜欢
          • 2020-06-13
          • 1970-01-01
          • 2021-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-15
          相关资源
          最近更新 更多