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

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

Labels length 2 should be 1 or 0

问题

我有这些数据:

  1. library(dplyr)
  2. 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"))
  3. dframekeep <- data.frame(id = c(1), name = c("Google"), date = c("27/8/2014 14:10:32"))

运行以下命令:

  1. b <- with(dframefull,
  2. aggregate(list(mytext=mytext),
  3. by=list(id=id,
  4. label=factor(I(date > dframekeep$date), labels=c("before", "after")),
  5. name=name),
  6. FUN=paste))

然后我收到以下错误:

  1. Error in factor(I(date > dframekeep$date), labels = c("before", "after")) :
  2. invalid 'labels'; length 2 should be 1 or 0
  3. In addition: Warning message:
  4. In Ops.factor(date, dframekeep$date) : > not meaningful for factors

有什么办法可以修复它吗?

预期结果是保留第二个数据框时间戳之前和之后10天,考虑到秒数:

  1. data.frame(id = c(1,1), label = c("before", "after"), name = c("Google", "Google"), mytext = c("text another text here", "other text text more"))
  2. id label name mytext
  3. 1 1 before Google text another text here
  4. 2 1 after Google other text text more
英文:

I have this data:

  1. library(dplyr)
  2. 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;))
  3. 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

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

And I receive this error:

  1. Error in factor(I(date &gt; dframekeep$date), labels = c(&quot;before&quot;, &quot;after&quot;)) :
  2. invalid &#39;labels&#39;; length 2 should be 1 or 0
  3. In addition: Warning message:
  4. 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

  1. 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;))
  2. id label name mytext
  3. 1 1 before Google text another text here
  4. 2 1 after Google other text text more

答案1

得分: 1

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

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

  1. library(dplyr)
  2. dframefull <- data.frame(id = c(1,1,1,1,1,1,1,1),
  3. name = c("Google", "Google", "Google", "Google",
  4. "Google", "Google", "Google", "Google"),
  5. date = c("12/8/2014 19:30:57", "26/8/2014 19:30:57",
  6. "27/8/2014 10:12:01", "27/8/2014 14:10:29",
  7. "27/8/2014 14:10:32", "27/8/2014 14:10:33",
  8. "3/9/2014 14:10:32", "14/9/2014 19:30:57"),
  9. mytext = c("out text", "text", "another", "text",
  10. "here", "other text", "text more",
  11. "out text 2"),
  12. stringsAsFactors = FALSE) %>%
  13. mutate(date = as.POSIXct(date,
  14. format = "%d/%m/%Y %H:%M:%S"))
  15. dframekeep <- data.frame(id = c(1),
  16. name = c("Google"),
  17. date = c("27/8/2014 14:10:32"),
  18. stringsAsFactors = FALSE) %>%
  19. mutate(date = as.POSIXct(date, format = "%d/%m/%Y %H:%M:%S"))
  20. b <- with(dframefull,
  21. aggregate(list(mytext=mytext),
  22. by=list(id=id,
  23. label=factor(I(date > dframekeep$date), labels=c("before", "after")),
  24. name=name),
  25. 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.

  1. library(dplyr)
  2. dframefull &lt;- data.frame(id = c(1,1,1,1,1,1,1,1),
  3. name = c(&quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;,
  4. &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;, &quot;Google&quot;),
  5. date = c(&quot;12/8/2014 19:30:57&quot;, &quot;26/8/2014 19:30:57&quot;,
  6. &quot;27/8/2014 10:12:01&quot;, &quot;27/8/2014 14:10:29&quot;,
  7. &quot;27/8/2014 14:10:32&quot;, &quot;27/8/2014 14:10:33&quot;,
  8. &quot;3/9/2014 14:10:32&quot;, &quot;14/9/2014 19:30:57&quot;),
  9. mytext = c(&quot;out text&quot;, &quot;text&quot;, &quot;another&quot;, &quot;text&quot;,
  10. &quot;here&quot;, &quot;other text&quot;, &quot;text more&quot;,
  11. &quot;out text 2&quot;),
  12. stringsAsFactors = FALSE) %&gt;%
  13. mutate(date = as.POSIXct(date,
  14. format = &quot;%d/%m/%Y %H:%M:%S&quot;))
  15. dframekeep &lt;- data.frame(id = c(1),
  16. name = c(&quot;Google&quot;),
  17. date = c(&quot;27/8/2014 14:10:32&quot;),
  18. stringsAsFactors = FALSE) %&gt;%
  19. mutate(date = as.POSIXct(date, format = &quot;%d/%m/%Y %H:%M:%S&quot;))
  20. b &lt;- with(dframefull,
  21. aggregate(list(mytext=mytext),
  22. by=list(id=id,
  23. label=factor(I(date &gt; dframekeep$date), labels=c(&quot;before&quot;, &quot;after&quot;)),
  24. name=name),
  25. 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:

确定