R 标签长度2应为1或0

R 标签长度2应为1或0,r,R,我有以下数据: 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/201

我有以下数据:

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"))
dframekeep <- data.frame(id = c(1), name = c("Google"), date = c("27/8/2014 14:10:32"))
你知道我该怎么修吗

预期结果是在考虑秒数的情况下,在第二个数据帧的时间戳之前和之后分别保持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

我对你的代码做了两处修改。首先,我在
data.frame
中使用了
stringsAsFactors=FALSE
参数。其次,我将
date
列转换为
POSIXct
类(日期/时间类)

这允许示例代码无错误地运行。我希望您的生产数据中的解决方案与此类似

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))
库(dplyr)
dframefull%
突变(日期=as.POSIXct(日期,
format=“%d/%m/%Y%H:%m:%S”))
数据帧保留%
变异(日期=as.POSIXct(日期,格式=“%d/%m/%Y%H:%m:%S”))
b dframekeep$date),labels=c(“之前”、“之后”),
名称=名称),
乐趣=粘贴)

预期结果是什么?在您的代码中,
dframefull
dframekeep
date
列是因素。也许你需要先将它们转换成
Date
对象?@Benjamin我使用了Date对象,但它没有考虑小时分钟seconds@Bruno更新了预期结果,我很抱歉。您需要使它们成为
POSIXct
对象。我很快就会提交一份答复。非常感谢。是的。还有一个问题,您知道如何在第二个数据帧的日期之前和之后保留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
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))