英文:
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 <- 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))
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("before", "after"), name = c("Google", "Google"), mytext = c("text another text here", "other text text more"))
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 %>%
dplyr::filter(
abs(difftime(date, dframekeep$date, units = "days")) <= 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)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
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
<sup>Created on 2020-01-06 by the reprex package (v0.3.0)</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论