Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R as_因子函数中的因子顺序异常_R_Forcats - Fatal编程技术网

R as_因子函数中的因子顺序异常

R as_因子函数中的因子顺序异常,r,forcats,R,Forcats,我试图使用as_factor()来按照因子在基础数据中的显示顺序创建因子级别。如果基础数据是字符,而不是数字,则函数可以正常工作 我尝试了使用as_factor文档给出的示例代码。它使用基础字符变量,并给出与基础变量相同的顺序;这就是Asu factor应该做的。但对于数值变量,顺序是排序的,as.factor和as_factor给出相同的顺序 # anomaly with as_factor -- is this a feature or a bug? # Bill Anderson Aug

我试图使用as_factor()来按照因子在基础数据中的显示顺序创建因子级别。如果基础数据是字符,而不是数字,则函数可以正常工作

我尝试了使用as_factor文档给出的示例代码。它使用基础字符变量,并给出与基础变量相同的顺序;这就是Asu factor应该做的。但对于数值变量,顺序是排序的,as.factor和as_factor给出相同的顺序

# anomaly with as_factor -- is this a feature or a bug?
# Bill Anderson  August 2019
require(tidyverse)
#> Loading required package: tidyverse
#> Warning: package 'tidyverse' was built under R version 3.6.1
#> Warning: package 'dplyr' was built under R version 3.6.1
# example from as_factor documentation
x <- c("a", "z", "g")
as_factor(x) # preserves input order, as desired
#> [1] a z g
#> Levels: a z g
as.factor(x) # factor levels obtained by sorting data
#> [1] a z g
#> Levels: a g z
# numeric example 
y <- c(1, 3, 2)
as_factor(y) # factor levels obtained by sorting data -- not what I expected
#> [1] 1 3 2
#> Levels: 1 2 3
as.factor(y) # factor levels obtained by sorting data
#> [1] 1 3 2
#> Levels: 1 2 3
identical(as_factor(y), as.factor(y))
#> [1] TRUE
# explicit character conversion
z <- as.character(y)
as_factor(z) # preserves input order, as desired
#> [1] 1 3 2
#> Levels: 1 3 2
as.factor(z) # factor levels obtained by sorting data
#> [1] 1 3 2
#> Levels: 1 2 3
# one can also put everything into a data frame, 
# so we can see the impact of the factor order is clearly visible
mtcars %>% group_by(cyl) %>%
  summarize(meandisp = mean(disp)) # cylinder order is sorted
#> # A tibble: 3 x 2
#>     cyl meandisp
#>   <dbl>    <dbl>
#> 1     4     105.
#> 2     6     183.
#> 3     8     353.
mtcars %>% group_by(as_factor(cyl)) %>%
  summarize(meandisp = mean(disp)) # cylinder order is still sorted
#> # A tibble: 3 x 2
#>   `as_factor(cyl)` meandisp
#>   <fct>               <dbl>
#> 1 4                    105.
#> 2 6                    183.
#> 3 8                    353.
mtcars %>% group_by(as_factor(as.character(cyl))) %>%
  summarize(meandisp = mean(disp)) # cylinder order follows data
#> # A tibble: 3 x 2
#>   `as_factor(as.character(cyl))` meandisp
#>   <fct>                             <dbl>
#> 1 6                                  183.
#> 2 4                                  105.
#> 3 8                                  353.
#带有as_因子的异常——这是一个特性还是一个bug?
#比尔·安德森2019年8月
要求(整洁的人)
#>装载所需包裹:tidyverse
#>警告:程序包“tidyverse”是在R版本3.6.1下构建的
#>警告:程序包“dplyr”是在R版本3.6.1下生成的
#as_因子文档中的示例
x[1]a z g
#>级别:a z g
as.因子(x)#通过排序数据获得的因子级别
#>[1]a z g
#>级别:a g z
#数值例子
y[1]13 2
#>级别:1 2 3
as.因子(y)#通过排序数据获得的因子级别
#> [1] 1 3 2
#>级别:1 2 3
相同(同系数(y),同系数(y))
#>[1]是的
#显式字符转换
z[1]13 2
#>级别:1 3 2
as.因子(z)#通过排序数据获得的因子级别
#> [1] 1 3 2
#>级别:1 2 3
#你也可以把所有的东西都放到一个数据框里,
#所以我们可以看到,因子顺序的影响是显而易见的
mtcars%%>%组别依据(气缸)%%>%
汇总(平均显示=平均显示)#气缸顺序已排序
#>#tibble:3 x 2
#>共青团
#>       
#> 1     4     105.
#> 2     6     183.
#> 3     8     353.
mtcars%>%分组依据(作为系数(气缸))%>%
汇总(平均显示=平均显示)#气缸顺序仍在排序中
#>#tibble:3 x 2
#>'as_factor(cyl)'meansip
#>                  
#> 1 4                    105.
#> 2 6                    183.
#> 3 8                    353.
mtcars%>%分组依据(作为因素(作为字符(cyl)))%>%
汇总(平均显示=平均显示)#气缸顺序遵循数据
#>#tibble:3 x 2
#>`as_factor(as.character(cyl))`meansip
#>                                
#> 1 6                                  183.
#> 2 4                                  105.
#> 3 8                                  353.
由(v0.3.0)于2019-08-2015年创建

没有错误消息。问题很简单,除非我显式地转换为字符,否则我无法获得底层数据的顺序


我不确定这是一个特性还是一个bug。但是如果它是一个特性,我建议它应该出现在函数文档中。

看起来这是有意让数值方法
作为\u因子
。链接提交使文档看起来已经更新以解释这一点,但可能只是在开发版本中