标签长度为2应为1或0。

huangapple go评论82阅读模式
英文:

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 &lt;- data.frame(id = c(1,1,1,1,1,1,1,1), name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;), date = c(&quot;12/8/2014 19:30:57&quot;, &quot;26/8/2014 19:30:57&quot;, &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;, &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;, &quot;3/9/2014 14:10:32&quot;, &quot;14/9/2014 19:30:57&quot;), mytext = c(&quot;out text&quot;, &quot;text&quot;, &quot;another&quot;, &quot;text&quot;, &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;, &quot;out text 2&quot;))
dframekeep &lt;- data.frame(id = c(1), name = c(&quot;Google&quot;), date = c(&quot;27/8/2014 14:10:32&quot;))

and running this commands

b &lt;- with(dframefull, 
           aggregate(list(mytext=mytext), 
                     by=list(id=id, 
                             label=factor(I(date &gt; dframekeep$date), labels=c(&quot;before&quot;, &quot;after&quot;)), 
                             name=name), 
                     FUN=paste))

And I receive this error:

Error in factor(I(date &gt; dframekeep$date), labels = c(&quot;before&quot;, &quot;after&quot;)) : 
  invalid &#39;labels&#39;; length 2 should be 1 or 0
In addition: Warning message:
In Ops.factor(date, dframekeep$date) : ‘&gt;’ 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(&quot;before&quot;, &quot;after&quot;), name = c(&quot;Google&quot;, &quot;Google&quot;), mytext = c(&quot;text another text here&quot;, &quot;other text text more&quot;))
  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 &lt;- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, 
                                  &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;), 
                         date = c(&quot;12/8/2014 19:30:57&quot;, &quot;26/8/2014 19:30:57&quot;, 
                                  &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;, 
                                  &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;, 
                                  &quot;3/9/2014 14:10:32&quot;,  &quot;14/9/2014 19:30:57&quot;), 
                         mytext = c(&quot;out text&quot;, &quot;text&quot;, &quot;another&quot;, &quot;text&quot;, 
                                    &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;, 
                                    &quot;out text 2&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, 
                           format = &quot;%d/%m/%Y %H:%M:%S&quot;))
dframekeep &lt;- data.frame(id = c(1), 
                         name = c(&quot;Google&quot;), 
                         date = c(&quot;27/8/2014 14:10:32&quot;),
                         stringsAsFactors = FALSE) %&gt;% 
  mutate(date = as.POSIXct(date, format = &quot;%d/%m/%Y %H:%M:%S&quot;))

b &lt;- with(dframefull, 
          aggregate(list(mytext=mytext), 
                    by=list(id=id, 
                            label=factor(I(date &gt; dframekeep$date), labels=c(&quot;before&quot;, &quot;after&quot;)), 
                            name=name), 
                    FUN=paste))

huangapple
  • 本文由 发表于 2020年1月7日 00:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定