使用`rows_append()`时添加因子水平。

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

Adding factor levels when using rows_append()

问题

当我使用 rows_append() 时,如何添加附加数据的级别以防止此错误:Error in rows_append():! Can't convert from...

我希望最终结果包括新组合数据集中的所有级别。

这是一个示例:

  1. library(dplyr)
  2. data1 <- iris[1:100,] %>%
  3. droplevels()
  4. data2 <- iris[101:150,] %>%
  5. droplevels()
  6. dataFull <- data1 %>%
  7. rows_append(data2)
  8. #> Error in `rows_append()`:
  9. #> ! Can't convert from `y$Species` <factor<23909>> to `x$Species` <factor<44a85>> due to loss of generality.
英文:

When I'm using rows_append() how do I add the levels of my additional data to prevent this error: Error in rows_append():! Can&#39;t convert from...

I would like the end result to include all levels in the newly combined data set.

Here's a reprex

  1. library(dplyr)
  2. data1 &lt;- iris[1:100,] %&gt;%
  3. droplevels()
  4. data2 &lt;- iris[101:150,] %&gt;%
  5. droplevels()
  6. dataFull &lt;- data1 %&gt;%
  7. rows_append(data2)
  8. #&gt; Error in `rows_append()`:
  9. #&gt; ! Can&#39;t convert from `y$Species` &lt;factor&lt;23909&gt;&gt; to `x$Species` &lt;factor&lt;44a85&gt;&gt; due to loss of generality.
  10. </details>
  11. # 答案1
  12. **得分**: 1
  13. Update removed first answer: (thanks to @Axeman):
  14. 我们可以合并因子水平并使它们在数据集之间保持一致
  15. ```R
  16. library(dplyr)
  17. levels <- union(levels(data1$Species), levels(data2$Species))
  18. data1$Species <- factor(data1$Species, levels = levels)
  19. data2$Species <- factor(data2$Species, levels = levels)
  20. data1 %>%
  21. rows_append(data2) %>%
  22. str()
  23. 'data.frame': 150 obs. of 5 variables:
  24. $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  25. $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  26. $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  27. $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  28. $ Species : Factor w/ 3 levels "setosa", "versicolor", ...: 1 1 1 1 1 1 1 1 1 1 ...
英文:

Update removed first answer: (thanks to @Axeman):

We could combine the factor levels and make them consistent across datasets

  1. library(dplyr)
  2. levels &lt;- union(levels(data1$Species), levels(data2$Species))
  3. data1$Species &lt;- factor(data1$Species, levels = levels)
  4. data2$Species &lt;- factor(data2$Species, levels = levels)
  5. data1 %&gt;%
  6. rows_append(data2) %&gt;%
  7. str()
  8. &#39;data.frame&#39;: 150 obs. of 5 variables:
  9. $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  10. $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  11. $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  12. $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  13. $ Species : Factor w/ 3 levels &quot;setosa&quot;,&quot;versicolor&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...

huangapple
  • 本文由 发表于 2023年4月11日 03:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980178.html
匿名

发表评论

匿名网友

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

确定