Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Keep specific date range before and after a timestamp

$
0
0

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

Viewing all articles
Browse latest Browse all 201839

Trending Articles