gpt4 book ai didi

r - 保留时间戳前后的特定日期范围

转载 作者:行者123 更新时间:2023-12-04 10:39:18 26 4
gpt4 key购买 nike

数据框结果如下:

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))

如何保留第二个数据帧的特定日期之前 10 天和之后 10 天?

这里是预期的输出

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

最佳答案

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

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

(尽管考虑到您的预期输出,我不确定这是否是您想要的)

关于r - 保留时间戳前后的特定日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com