英文:
Labels length 2 should be 1 or 0
问题
我有这些数据:
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"))
运行以下命令:
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))
然后我收到以下错误:
Error in factor(I(date > dframekeep$date), labels = c("before", "after")) :
invalid 'labels'; length 2 should be 1 or 0
In addition: Warning message:
In Ops.factor(date, dframekeep$date) : ‘>’ not meaningful for factors
有什么办法可以修复它吗?
预期结果是保留第二个数据框时间戳之前和之后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
英文:
I have this data:
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"))
and running this commands
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))
And I receive this error:
Error in factor(I(date > dframekeep$date), labels = c("before", "after")) :
invalid 'labels'; length 2 should be 1 or 0
In addition: Warning message:
In Ops.factor(date, dframekeep$date) : ‘>’ not meaningful for factors
Any idea how can I fix it?
Expected result is to keep 10 days before and 10 days after the time stamp of the second dataframe taking into considaration the seconds
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
得分: 1
我对你的代码进行了两个更改。首先,在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))
英文:
I made two changes to your code. First, I used the stringsAsFactors = FALSE
argument in data.frame
. Second, I converted your date
columns to POSIXct
class (date/time class).
This allows your example code to run without error. I hope the solution in your production data is similar.
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论