【问题标题】:Check if date is between two dates in another data frame, and manipulate date if it is检查日期是否在另一个数据框中的两个日期之间,如果是则操作日期
【发布时间】:2019-08-14 20:29:22
【问题描述】:

我有两个数据框(df1 和 df2);它们每个都有一个 ID 列,并按 ID 号组织,每个数据帧的每个 ID 有很多行。 df1 有一个“unique_posix”列,df2 有一个“date.time.start”和“date.time.end”列,还有一个“depth”和“shape”列。 对于每个 ID,我想从 df1 中获取我的“unique_posix”列并转到 df2 并找到它介于两者之间或之上的“date.time.start”和“date.time.end”。当我找到它对应的行时,我想从 df2 中提取“深度”和“形状”,并将其复制到 df1 中该唯一日期/时间的新列。

我已经尝试使用 if/else 作为 for 循环来执行此操作,并且我已尝试在 dplyr 中执行此操作。

df1<-data.frame(ID=c('SW12','SW12','SW12','SW12','SW12','SW13','SW13','SW13','SW13','SW13'), unique_posix=c('5/3/10 16:47','5/3/10 16:53','5/3/10 17:00', '5/3/10 18:00','5/3/10/ 18:12','8/15/10 17:13','8/15/10 17:18','8/15/10 17:37','8/15/10 18:00','8/15/10 18:52'))

df2<- data.frame(ID=c('SW12','SW12','SW12','SW12','SW12','SW13','SW13','SW13','SW13','SW13'), Date.Time.Start=c('5/3/10 15:57','5/3/10 16:18', '5/3/10 16:55','5/3/10 17:36','5/3/10 18:17','8/15/10 16:55','8/15/10 17:28','8/15/10 17:54', '8/15/10 18:55','8/15/10 19:20'), Date.Time.End=c('5/3/10 16:09','5/3/10 16:44','5/3/10 17:28', '5/3/10 18:08', '5/3/10 18:49', '8/15/10 17:22', '8/15/10 17:52','8/15/10 18:06','8/15/10 19:15','8/15/10 19:40'), Shape=c('U','U','V','Square','U','U','U','Square','V','U'), Depth=c(1,2,3,4,5,6,7,8,9,10))

我希望 df1 最终看起来像:

df1b<-data.frame(ID=c('SW12','SW12','SW12','SW12','SW12','SW13','SW13','SW13','SW13','SW13'), unique_posix=c('5/3/10 16:47','5/3/10 16:53','5/3/10 17:00', '5/3/10 18:00','5/3/10/ 18:12','8/15/10 17:13','8/15/10 17:18','8/15/10 17:37','8/15/10 18:00','8/15/10 18:52'), Dive.Shape=c(NA,NA,'V','Square',NA,'U','U','U','Square', NA),Dive.Depth=c(NA,NA,3,4,NA,6,6,7,8,NA))

我已将日期/时间转换为 POSIXct/lt:

library(dplyr)
df1 <- df1 %>% 
  mutate(
    ID = factor(ID),
    unique_posix = mdy_hm(unique_posix)
  )
class(df1$unique_posix)

df2 <- df2 %>% 
  mutate(
    ID = factor(ID),
    Date.Time.Start = mdy_hm(Date.Time.Start),
    Date.Time.End = mdy_hm(Date.Time.End)
  )
class(df2$Date.Time.Start)

作为一个for循环我试过了:

df1b<-df1
for (i in 1:nrow(df1)) {
  if (df1$unique_posix %within% interval(df2$Date.Time.Start, df2$Date.Time.End)) {
    df1b$Dive.Shape<-df2$Shape
    df1b$Dive.Depth<-df2$Depth
  }
  else {
    df1b$Dive.Shape<-NA
    df2b$Dive.Depth<-NA
  }
}

在 dplyr 我正在尝试这样的事情:

df1b<-inner_join(df1, df2, by="DeployID")
df1b %>% rowwise() %>%
  mutate(Dive.Shape=ifelse(between(unique_posix, Date.Time.Start, Date.Time.End),Shape,NA )) %>%
mutate(Dive.Depth=ifelse(between(unique_posix, Date.Time.Start, Date.Time.End),Depth,NA ))
  arrange(DeployID,desc(unique_posix)) %>%
  distinct(unique_posix)

这些似乎都不起作用,但我觉得我很接近?

我希望我的 df1b 有两个额外的 Dive.Shape 和 Dive.Depth 列,如果 unique_posix 日期/时间不在 Date.Time 之内或之上,则它将包含一个“NA”。 df2 框架中的 Start 和 Date.Time.End 范围[对于每个 ID]。如果 df1 的 unique_posix 介于 df2 的 Date.Time.Start 或 Date.Time.End 列之间或之上,则这些列将包含来自 df2 的 Shape 和 df2 的 Depth 列的值。

感谢您为我提供的任何帮助!

【问题讨论】:

    标签: r function datetime dplyr between


    【解决方案1】:

    对于data.table,使用非等更新连接相对简单:

    library(data.table)
    setDT(df1)
    setDT(df2)
    
    df1[df2
        , on = .(ID
                 , unique_posix > Date.Time.Start
                    , unique_posix < Date.Time.End)
        , `:=`(Dive.Shape = Shape, Dive.Depth = Depth)]
    
    df1
    
    > df1
          ID        unique_posix Dive.Shape Dive.Depth
     1: SW12 2010-05-03 16:47:00       <NA>         NA
     2: SW12 2010-05-03 16:53:00       <NA>         NA
     3: SW12 2010-05-03 17:00:00          V          3
     4: SW12 2010-05-03 18:00:00     Square          4
     5: SW12 2010-05-03 18:12:00       <NA>         NA
     6: SW13 2010-08-15 17:13:00          U          6
     7: SW13 2010-08-15 17:18:00          U          6
     8: SW13 2010-08-15 17:37:00          U          7
     9: SW13 2010-08-15 18:00:00     Square          8
    10: SW13 2010-08-15 18:52:00       <NA>         NA
    

    另请参阅:How to do a data.table rolling join?

    【讨论】:

    • 如果它有效,您可以投票并选择它作为解决方案。
    【解决方案2】:

    我想你是。问题是在 data.frames 中,日期/时间保存为字符。

    apply(df1, 2, class) 
              ID unique_posix 
    >  "character"  "character" 
    
    apply(df2, 2, class)
                 ID Date.Time.Start   Date.Time.End           Shape           Depth 
        "character"     "character"     "character"     "character"     "character" 
    

    实际上,您希望将unique_posixDate.Time.StartDate.Time.End 转换为日期/时间。可能使用strptime()?我认为比较会起作用,但我还没有验证它们。我需要尽快去,但我还是想给你一些东西。

    【讨论】:

    • 对不起,我之前没有指定,我确实先转换为日期和时间!我已经在上面添加了该代码!
    【解决方案3】:

    如果你仍然想追求 dplyr 解决方案,试试这个:

    inner_join(df1, df2, by = "ID") %>%
      rowwise() %>%
      filter (between(unique_posix, Date.Time.Start, Date.Time.End)) %>%
      right_join(df1, by = c("ID", "unique_posix")) %>%
      dplyr::select (-c(Date.Time.Start, Date.Time.End), Dive.Shape = Shape, Dive.Depth = Depth)
    

    【讨论】:

    • 我在尝试运行时遇到一个奇怪的错误:Joining, by = c("ID", "unique_posix", "Lon", "Lat", "lc", "Bottom .Depth”、“cgoa_bathy”、“pacific_GE”)(函数(类、fdef、mtable)中的错误:无法为签名“tbl_df”的函数“select”找到继承的方法另外:警告消息:1:列ID 加入不同级别的因子,强制转换为字符向量 这可能与我的数据框中有其他列在我的示例中没有使用的事实有关,所以我会看看它是否有效我减少数据框...
    • 我明白了 - 我没有意识到还有其他变量。我可以通过 ID 和 unique_posix 更新我的答案以明确正确加入。还应该逐行添加,并为您的错误添加 dplyr for select(可能是另一个带有 select 的包)。对此感到抱歉。
    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2017-07-07
    相关资源
    最近更新 更多