简体   繁体   English

按特定列值过滤日期

[英]Filter dates by particular column value

I am trying to filter dates with a particular offsetdate value from calendar days dataframe.我正在尝试从日历日 dataframe 中过滤具有特定 offsetdate 值的日期。

Dataframe calendardays: Dataframe 日历日:

calendardays <- as.data.frame(as.Date(seq(as.Date("2019-01-03"), as.Date("2022-12-31"), by="days")))
colnames(calendardays)<- c("Date")

DF2: DF2:

     LC    Custcode       Date      offsetdate
    RM11    IN007543    2019-10-03  2
    WK15    IN007543    2019-10-03  3

In DF2, for each row there is a offsetdate which means that I want to find x no.在 DF2 中,对于每一行都有一个 offsetdate,这意味着我想找到 x 号。 of dates from the Date which is mentioned in DF2 corresponding to that LC CustCode from Dataframe calendardays. DF2 中提到的日期对应于 Dataframe calendardays 中的 LC CustCode 的日期。

Output: Output:

     LC    Custcode       Date      
    RM11    IN007543    2019-10-03
    RM11    IN007543    2019-10-04
    WK15    IN007543    2019-10-03
    WK15    IN007543    2019-10-04
    WK15    IN007543    2019-10-05  

Expand DF2 using ix and then increment the Date field.使用 ix 展开 DF2,然后增加 Date 字段。 No packages are used.不使用任何包。

ix <- rep(1:nrow(DF2), DF2$offsetdate) # 1,1,2,2,2
DF2rep <- transform(DF2[ix, ], Date = Date + sequence(DF2$offsetdate) - 1)[-4]

This already gives the output shown in the question but if you need to check whether the dates also appear in calendardays this will only keep dates in DF2rep that are also in calendardays.这已经给出了问题中显示的 output 但如果您需要检查日期是否也出现在日历日中,这只会保留 DF2rep 中的日期,这些日期也在日历日中。

DF2rep[DF2rep$Date %in% calendardays$Date, ]

Note笔记

DF2 in reproducible form is assumed to be:假设可重现形式的 DF2 为:

Lines <- "     LC    Custcode       Date      offsetdate
    RM11    IN007543    2019-10-03  2
    WK15    IN007543    2019-10-03  3"
DF2 <- read.table(text = Lines, header = TRUE)
DF2$Date <- as.Date(DF2$Date)

Why do we need calendardays ?为什么我们需要calendardays We can directly expand DF2 using tidyverse我们可以使用tidyverse直接扩展DF2

library(tidyverse)

DF2 %>%
  mutate(Date = as.Date(Date), 
         seq_day = map2(Date, Date + offsetdate, seq, by = "1 day")) %>%
  unnest(seq_day) %>%
  select(-Date, -offsetdate)

#  LC    Custcode seq_day   
#  <fct> <fct>    <date>    
#1 RM11  IN007543 2019-10-03
#2 RM11  IN007543 2019-10-04
#3 RM11  IN007543 2019-10-05
#4 WK15  IN007543 2019-10-03
#5 WK15  IN007543 2019-10-04
#6 WK15  IN007543 2019-10-05
#7 WK15  IN007543 2019-10-06

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 过滤以删除特定列中特定值之前的所有行,而该特定值出现多次 - Filter to remove all rows before a particular value in a specific column, while this particular value occurs several time 过滤以在特定列中的特定值第一次出现之前删除所有行 - Filter to remove all rows before the first time a particular value in a specific column appears 如何按存储在变量中的列中的日期过滤 dataframe? - How to filter dataframe by dates in a column stored in variable? 将特定列中每行的值转换为该特定列中特定行的值的百分比 - Convert the value of each row in a particular column into a percentage of the value of a particular row in that particular column 如果另一列包含值,则过滤列值 - Filter for column value if another column contains a value 如何在特定列的行中向特定 position 添加特定值? - How to add a particular value to a particular position in rows of a specific column? 对于列中的每个唯一值,请查找至少4个日期 - For every unique value in column find the min 4 dates Select 日期基于其他列的值 - Select dates based on value of other column 替换与R中的特定模式匹配的列中的值 - Replace a value in a column matching a particular pattern in R 如何从R中的特定列中选择值 - How to pick value from a particular column in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM