在时间戳之前和之后保留特定日期范围

huangapple go评论114阅读模式
英文:

Keep specific date range before and after a timestamp

问题

以下是翻译的代码部分:

library(dplyr)
dframefull <- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c("Google", "Google", "Google", "Google", 
                                  "Google", "Google", "Google", "Google"), 
                         date = c("12/8/2014 19:30:57", "26/8/2014 19:30:57", 
                                  "27/8/2014 10:12:01", "27/8/2014 14:10:29", 
                                  "27/8/2014 14:10:32", "27/8/2014 14:10:33", 
                                  "3/9/2014 14:10:32",  "14/9/2014 19:30:57"), 
                         mytext = c("out text", "text", "another", "text", 
                                    "here", "other text", "text more", 
                                    "out text 2"),
                         stringsAsFactors = FALSE) %>%
  mutate(date = as.POSIXct(date, 
                           format = "%d/%m/%Y %H:%M:%S"))
dframekeep <- data.frame(id = c(1), 
                         name = c("Google"), 
                         date = c("27/8/2014 14:10:32"),
                         stringsAsFactors = FALSE) %>%
  mutate(date = as.POSIXct(date, format = "%d/%m/%Y %H:%M:%S"))

b <- with(dframefull, 
          aggregate(list(mytext=mytext), 
                    by=list(id=id, 
                            label=factor(I(date > dframekeep$date), labels=c("before", "after")), 
                            name=name), 
                    FUN=paste))

请注意,以上是代码部分的翻译,如果您需要翻译其他部分或有任何其他问题,请随时提出。

英文:

Having a dataframe result like this:

library(dplyr)
dframefull &lt;- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, 
                                  &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;), 
                         date = c(&quot;12/8/2014 19:30:57&quot;, &quot;26/8/2014 19:30:57&quot;, 
                                  &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;, 
                                  &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;, 
                                  &quot;3/9/2014 14:10:32&quot;,  &quot;14/9/2014 19:30:57&quot;), 
                         mytext = c(&quot;out text&quot;, &quot;text&quot;, &quot;another&quot;, &quot;text&quot;, 
                                    &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;, 
                                    &quot;out text 2&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, 
                           format = &quot;%d/%m/%Y %H:%M:%S&quot;))
dframekeep &lt;- data.frame(id = c(1), 
                         name = c(&quot;Google&quot;), 
                         date = c(&quot;27/8/2014 14:10:32&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, format = &quot;%d/%m/%Y %H:%M:%S&quot;))

b &lt;- with(dframefull, 
          aggregate(list(mytext=mytext), 
                    by=list(id=id, 
                            label=factor(I(date &gt; dframekeep$date), labels=c(&quot;before&quot;, &quot;after&quot;)), 
                            name=name), 
                    FUN=paste))

How is it possible to keep 10 day before and 10 days after the specific date of second dataframe?

Here an expected output

data.frame(id = c(1,1), label = c(&quot;before&quot;, &quot;after&quot;), name = c(&quot;Google&quot;, &quot;Google&quot;), mytext = c(&quot;text another text here&quot;, &quot;other text text more&quot;))
  id  label   name                 mytext
1  1 before Google text another text here
2  1  after Google   other text text more

答案1

得分: 3

如果您只有dframekeep中的一个日期,您可以按如下方式筛选dframefull的行:

dframefull %>%
    dplyr::filter(
        abs(difftime(date, dframekeep$date, units = "days")) <= 10
    )

(虽然我不确定这是否符合您的预期输出)

英文:

If you only have the one date in dframekeep you can filter the rows of dframefull as follows:

dframefull %&gt;% 
    dplyr::filter(
        abs(difftime(date, dframekeep$date, units = &quot;days&quot;)) &lt;= 10
    )

(Although I am not sure if this is what you want, given your expected output)

答案2

得分: 2

这里是代码的翻译部分:

library(tidyverse)
library(lubridate)

dframefull <- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c("Google", "Google", "Google", "Google", 
                                  "Google", "Google", "Google", "Google"), 
                         date = c("12/8/2014 19:30:57", "26/8/2014 19:30:57", 
                                  "27/8/2014 10:12:01", "27/8/2014 14:10:29", 
                                  "27/8/2014 14:10:32", "27/8/2014 14:10:33", 
                                  "3/9/2014 14:10:32",  "14/9/2014 19:30:57"), 
                         mytext = c("out text", "text", "another", "text", 
                                    "here", "other text", "text more", 
                                    "out text 2"),
                         stringsAsFactors = FALSE) %>%
  mutate(date = as.POSIXct(date, 
                           format = "%d/%m/%Y %H:%M:%S"))
dframekeep <- data.frame(id = c(1), 
                         name = c("Google"), 
                         date = c("27/8/2014 14:10:32"),
                         stringsAsFactors = FALSE) %>%
  mutate(date = as.POSIXct(date, format = "%d/%m/%Y %H:%M:%S"))

dframekeep2 <- dframekeep %>%
  mutate(start_date = date - days(10),
         end_date = date + days(10))

dframefull %>%
  fuzzyjoin::fuzzy_semi_join(dframekeep2, by = c("date" = "start_date",
                                                "date" = "end_date"), match_fun = list(`>`,`<`))
#>   id   name                date     mytext
#> 2  1 Google 2014-08-26 19:30:57       text
#> 3  1 Google 2014-08-27 10:12:01    another
#> 4  1 Google 2014-08-27 14:10:29       text
#> 5  1 Google 2014-08-27 14:10:32       here
#> 6  1 Google 2014-08-27 14:10:33 other text
#> 7  1 Google 2014-09-03 14:10:32  text more

before_df <- dframefull %>%
  fuzzyjoin::fuzzy_semi_join(dframekeep2, by = c("date" = "start_date","date" = "date"), match_fun = list(`>`,`<=`)) %>%
  mutate(label = "before")

after_df <- dframefull %>%
  fuzzyjoin::fuzzy_semi_join(dframekeep2, by = c("date" = "end_date","date" = "date"), match_fun = list(`<`,`>=`)) %>%
  mutate(label = "after")

before_df %>%
  bind_rows(after_df) %>%
  select(-date) %>%
  as_tibble() %>%
  select(-id) %>%
  pivot_wider(names_from = label, values_from = mytext, values_fn = list(mytext = ~ reduce(.,
                                                                                        str_c,
                                                                                        sep = " "))) %>%
  pivot_longer(before:after, names_to = "label", values_to = "mytext")
#> # A tibble: 2 x 3
#>   name   label  mytext                   
#>   <chr>  <chr>  <chr>                    
#> 1 Google before text another text here   
#> 2 Google after  here other text text more

创建于2020-01-06,使用reprex包 (v0.3.0)

英文:

Ok here it is

library(tidyverse)
library(lubridate)
#&gt; 
#&gt; Attaching package: &#39;lubridate&#39;
#&gt; The following object is masked from &#39;package:base&#39;:
#&gt; 
#&gt;     date

dframefull &lt;- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, 
                                  &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;), 
                         date = c(&quot;12/8/2014 19:30:57&quot;, &quot;26/8/2014 19:30:57&quot;, 
                                  &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;, 
                                  &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;, 
                                  &quot;3/9/2014 14:10:32&quot;,  &quot;14/9/2014 19:30:57&quot;), 
                         mytext = c(&quot;out text&quot;, &quot;text&quot;, &quot;another&quot;, &quot;text&quot;, 
                                    &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;, 
                                    &quot;out text 2&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, 
                           format = &quot;%d/%m/%Y %H:%M:%S&quot;))
dframekeep &lt;- data.frame(id = c(1), 
                         name = c(&quot;Google&quot;), 
                         date = c(&quot;27/8/2014 14:10:32&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, format = &quot;%d/%m/%Y %H:%M:%S&quot;))

dframekeep2 &lt;- dframekeep %&gt;%
  mutate(start_date = date - days(10),
         end_date = date + days(10))

dframefull %&gt;% 
  fuzzyjoin::fuzzy_semi_join(dframekeep2,by = c(&quot;date&quot; = &quot;start_date&quot;,
                                                &quot;date&quot; = &quot;end_date&quot;),match_fun = list(`&gt;`,`&lt;`))
#&gt;   id   name                date     mytext
#&gt; 2  1 Google 2014-08-26 19:30:57       text
#&gt; 3  1 Google 2014-08-27 10:12:01    another
#&gt; 4  1 Google 2014-08-27 14:10:29       text
#&gt; 5  1 Google 2014-08-27 14:10:32       here
#&gt; 6  1 Google 2014-08-27 14:10:33 other text
#&gt; 7  1 Google 2014-09-03 14:10:32  text more

before_df &lt;- dframefull %&gt;% 
  fuzzyjoin::fuzzy_semi_join(dframekeep2,by = c(&quot;date&quot; = &quot;start_date&quot;,&quot;date&quot; = &quot;date&quot;),match_fun = list(`&gt;`,`&lt;=`)) %&gt;%
  mutate(label = &quot;before&quot;)

after_df &lt;- dframefull %&gt;% 
  fuzzyjoin::fuzzy_semi_join(dframekeep2,by = c(&quot;date&quot; = &quot;end_date&quot;,&quot;date&quot; = &quot;date&quot;),match_fun = list(`&lt;`,`&gt;=`)) %&gt;%
  mutate(label = &quot;after&quot;)

before_df %&gt;% 
  bind_rows(after_df) %&gt;%
  select(-date) %&gt;% 
  as_tibble() %&gt;% 
  select(-id) %&gt;% 
  pivot_wider(names_from = label,values_from = mytext,values_fn =list(mytext = ~ reduce(.,
                                                                                        str_c,
                                                                                        sep = &quot; &quot;))) %&gt;% 
  pivot_longer(before:after,names_to = &quot;label&quot;,values_to = &quot;mytext&quot;)
#&gt; # A tibble: 2 x 3
#&gt;   name   label  mytext                   
#&gt;   &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;                    
#&gt; 1 Google before text another text here   
#&gt; 2 Google after  here other text text more

<sup>Created on 2020-01-06 by the reprex package (v0.3.0)</sup>

huangapple
  • 本文由 发表于 2020年1月7日 01:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定