Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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
当我使用factor()而不设置级别时,为什么输出不按字母顺序排列?_R - Fatal编程技术网

当我使用factor()而不设置级别时,为什么输出不按字母顺序排列?

当我使用factor()而不设置级别时,为什么输出不按字母顺序排列?,r,R,这是我的代码: library(tidyverse) x <- c("Jan","Aug", "Feb") sort(x) #> [1] "Aug" "Feb" "Jan" factor(x) #> [1] Jan Aug Feb #> Levels: Aug Feb Jan 库(tidyverse) x[1]“八月”“二月”“一月” 系数(x) #>[1]一月至八月至二月 #>级别:8月至2月至1月 我知道排序函数将按字母顺序排列x。我认为,如果我只计算(x),但不

这是我的代码:

library(tidyverse)
x <- c("Jan","Aug", "Feb")
sort(x)
#> [1] "Aug" "Feb" "Jan"
factor(x)
#> [1] Jan Aug Feb
#> Levels: Aug Feb Jan
库(tidyverse)
x[1]“八月”“二月”“一月”
系数(x)
#>[1]一月至八月至二月
#>级别:8月至2月至1月

我知道排序函数将按字母顺序排列x。我认为,如果我只计算(x),但不向它传递任何级别,它仍然会按字母顺序排列(默认方式)。但结果是上面代码中所示的结果。我的意思是,为什么结果不是按字母顺序排列的?提前谢谢大家

否,
级别
因子
的打印顺序没有关系<代码>因子不会改变向量的顺序<除非明确提及,
因素的编码>级别
通常按字母顺序排列。比如说,

let <- letters[1:10]
factor(let)
#[1] a b c d e f g h i j
#Levels: a b c d e f g h i j
分配随机级别

set.seed(123)
factor(let, levels = sample(let))
#[1] a b c d e f g h i j
#Levels: c j b h f i a g e d

正如我们可以看到的,在所有情况下,
级别
都不同,但无论
级别
如何,因子的打印方式都不会改变,因为
因子
不会改变顺序

因为
factor
不会改变顺序,它会将
x
转换为
factor
,级别按字母顺序排列。但为什么输出显示级别按字母顺序排列?既然它显示级别是按字母顺序排列的,那么不应该按这种方式排列吗?我知道了!非常感谢。
set.seed(123)
factor(let, levels = sample(let))
#[1] a b c d e f g h i j
#Levels: c j b h f i a g e d