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

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

Adding factor levels when using rows_append()

问题

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

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

这是一个示例:

library(dplyr)

data1 <- iris[1:100,] %>%
  droplevels()
data2 <- iris[101:150,] %>%
  droplevels()

dataFull <- data1 %>%
  rows_append(data2)
#> Error in `rows_append()`:
#> ! 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

library(dplyr)

data1 &lt;- iris[1:100,] %&gt;%
  droplevels()
data2 &lt;- iris[101:150,] %&gt;%
  droplevels()

dataFull &lt;- data1 %&gt;%
  rows_append(data2)
#&gt; Error in `rows_append()`:
#&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.





</details>


# 答案1
**得分**: 1

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

我们可以合并因子水平并使它们在数据集之间保持一致

```R
library(dplyr)

levels <- union(levels(data1$Species), levels(data2$Species))

data1$Species <- factor(data1$Species, levels = levels)
data2$Species <- factor(data2$Species, levels = levels)

data1 %>% 
  rows_append(data2) %>% 
  str()

'data.frame': 150 obs. of 5 variables:
 $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ 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

library(dplyr)

levels &lt;- union(levels(data1$Species), levels(data2$Species))

data1$Species &lt;- factor(data1$Species, levels = levels)
data2$Species &lt;- factor(data2$Species, levels = levels)

data1 %&gt;% 
  rows_append(data2) %&gt;% 
  str()

&#39;data.frame&#39;:	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ 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:

确定