【问题标题】:Keep rows until a specific timestamp even if the last one does not exists保留行直到特定时间戳,即使最后一个不存在
【发布时间】:2019-11-02 00:02:44
【问题描述】:

拥有一个提供特定时间戳的数据框

dframe1 <- structure(list(id = c(1L, 1L, 1L, 2L, 2L), name = c("Google", 
"Yahoo", "Amazon", "Amazon", "Google"), date = c("2008-11-01", 
"2008-11-01", "2008-11-04", "2008-11-01", "2008-11-02")), class = "data.frame", row.names = c(NA, 
-5L))

第二个我想在第一个数据帧的特定时间前后保留信息

dframe2 <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L), date = c("2008-11-01", "2008-11-01", 
"2008-11-04", "2008-10-31", "2008-10-31", "2008-11-02", "2008-11-02", 
"2008-11-02", "2008-11-05", "2008-11-02", "2008-11-03", "2008-10-31", 
"2008-11-01", "2008-11-01", "2008-11-02", "2008-11-02", "2008-11-03"
), text_sth = c("test", "text_sth", "text here", "another text", 
"other", "another one", "test", "text_sth", "text here", "another text", 
"other", "etc", "test", "text_sth", "text here", "another text", 
"text here")), row.names = c(NA, -17L), class = "data.frame")

怎么可能有这个输出?

id                               text_sth   name label
1                     another text other Google   before
1 another one test text_sth another text Google after
1                     another text other  Yahoo   before
1 another one test text_sth another text  Yahoo after
1                                  other Amazon   before
1                              text here Amazon after

使用此代码,它仅在找到时间戳前后两天时才保留结果。即使之前和之后的时间戳不存在但之前的所有日子都存在,如何更改它并使其保持到前后两天?来自here

left_join(dframe1, df2, by = "id") %>% 
  mutate(date_diff = as.numeric(date.y - date.x)) %>%
  filter(abs(date_diff) == 2) %>% 
  mutate(label = ifelse(date_diff == -2, "before", "after")) %>% 
  select(id, name, label, text_sth)

【问题讨论】:

    标签: r


    【解决方案1】:

    下次可能会指出你的previous post并包含相关的代码位,例如,你需要先转换数据(像以前一样)

    dframe1$date = as.Date(dframe1$date)
    dframe2$date = as.Date(dframe2$date)
    

    因此,使用 @IaroslavDomin 提供的很好的功能,您需要更改过滤器。我在这里所做的与他的有些不同。我直接用dframe2。

    X = left_join(dframe1, dframe2, by = "id") %>% 
      mutate(date_diff = as.numeric(date.y - date.x)) %>%
      # change the filter here, >0 means not the same
      # < 2 means within 2 days 
      filter(abs(date_diff)>0 & abs(date_diff)<2 ) %>% 
      mutate(label = ifelse(date_diff <0, "before", "after")) %>% 
      select(id, name, label, text_sth)
    

    现在我们有了带有标签的文本。我们将它们分组并给出适当的标签

    X= X %>% group_by(id,name,label) %>%
    summarize(test=paste(unique(text_sth),collapse=" "))
    

    如果我们进入决赛桌:

    # A tibble: 10 x 4
    # Groups:   id, name [5]
          id name   label  test                                  
       <int> <chr>  <chr>  <chr>                                 
     1     1 Amazon after  text here                             
     2     1 Amazon before other                                 
     3     1 Google after  another one test text_sth another text
     4     1 Google before another text other                    
     5     1 Yahoo  after  another one test text_sth another text
     6     1 Yahoo  before another text other                    
     7     2 Amazon after  text here another text                
     8     2 Amazon before etc                                   
     9     2 Google after  text here                             
    10     2 Google before test text_sth  
    

    【讨论】:

    • 谢谢。我检查了它,但结果也保留了输入数据框中的值
    • 什么意思?我已经编辑了帖子以显示结果。哪一部分与您的预期不同?
    • 实际上,如果您使用此选项,您将在特定日期之前再次使用它不会考虑例如如果我少于 4 天有两天内的数据
    • 好的,所以您想要之前和之后的所有内容?你的问题不是很清楚。你想要的输出是 2 天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多