如何在R中打印没有级别的因子向量?

要在 R 中打印没有级别的因子向量,我们可以按照以下步骤操作 -

  • 首先,创建一个因子向量。

  • 然后,使用as.character函数读取向量。

示例 1

创建因子向量

让我们创建一个名为 f1 的向量,如下所示 -

f1<-factor(sample(LETTERS[1:3],20,replace=TRUE))

f1

执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-

[1] A B B A C C C B C B B A A B B C B A A C

Levels: A B C

打印没有水平的因子向量

使用as.character函数打印没有级别的因子向量 f1 -

f1<-factor(sample(LETTERS[1:3],20,replace=TRUE))

as.character(f1)

输出

[1] "A" "B" "B" "A" "C" "C" "C" "B" "C" "B" "B" "A" "A" "B" "B" "C" "B" "A" "A"

[20] "C"

示例 2

创建因子向量

让我们创建一个名为 f2 的向量,如下所示 -

f2<-factor(sample(c("Male","Female"),120,replace=TRUE))

f2

[1] Male Male Female Female Female Female Male Female Female Male

[11] Male Female Female Female Female Male Female Female Female Female

[21] Male Male Male Female Female Female Female Male Male Female

[31] Female Male Male Male Male Male Male Male Male Female

[41] Female Male Male Female Male Male Male Female Female Male

[51] Female Male Female Male Female Male Female Female Male Female

[61] Male Male Female Female Male Female Female Male Female Male

[71] Male Male Female Female Female Male Female Female Female Male

[81] Female Male Male Male Male Female Male Male Female Male

[91] Male Male Female Male Female Male Male Female Male Female

[101] Male Female Female Female Male Male Male Male Male Female

[111] Male Female Male Female Female Male Female Female Male Male

Levels: Female Male

打印没有水平的因子向量

使用as.character函数打印没有级别的因子向量 f2 -

f2<-factor(sample(c("Male","Female"),120,replace=TRUE))

as.character(f2)

输出

[1] "Male" "Male" "Female" "Female" "Female" "Female" "Male" "Female"

[9] "Female" "Male" "Male" "Female" "Female" "Female" "Female" "Male"

[17] "Female" "Female" "Female" "Female" "Male" "Male" "Male" "Female"

[25] "Female" "Female" "Female" "Male" "Male" "Female" "Female" "Male"

[33] "Male" "Male" "Male" "Male" "Male" "Male" "Male" "Female"

[41] "Female" "Male" "Male" "Female" "Male" "Male" "Male" "Female"

[49] "Female" "Male" "Female" "Male" "Female" "Male" "Female" "Male"

[57] "Female" "Female" "Male" "Female" "Male" "Male" "Female" "Female"

[65] "Male" "Female" "Female" "Male" "Female" "Male" "Male" "Male"

[73] "Female" "Female" "Female" "Male" "Female" "Female" "Female" "Male"

[81] "Female" "Male" "Male" "Male" "Male" "Female" "Male" "Male"

[89] "Female" "Male" "Male" "Male" "Female" "Male" "Female" "Male"

[97] "Male" "Female" "Male" "Female" "Male" "Female" "Female" "Female"

[105] "Male" "Male" "Male" "Male" "Male" "Female" "Male" "Female"

[113] "Male" "Female" "Female" "Male" "Female" "Female" "Male" "Male"

示例 3

创建因子向量

让我们创建一个名为 f3 的向量,如下所示 -

f3<-factor(sample(c("Low","Medium","High"),120,replace=TRUE))

f3

[1] Medium High Medium High Medium High Low Low High Medium

[11] High Low High Medium Medium Low Medium Low High High

[21] Medium Medium Low Low High Low High Medium High Medium

[31] Low Low High High Low Low High Low Low High

[41] High Low High High Low High High Medium High Medium

[51] High Medium Low Medium Low Low High High High High

[61] Low Low High High Low Low High High High High

[71] Low Low Medium High Low Low Medium High Low Medium

[81] High Medium Medium Medium High Medium Low Medium Low High

[91] High Medium High Low Medium Low Low High Medium Low

[101] High Medium Low High High Low Medium Medium Medium Low

[111] High Low Medium High High High Medium Low Low Low

Levels: High Low Medium

打印没有水平的因子向量

使用as.character函数打印没有级别的因子向量 f3 -

f3<-factor(sample(c("Low","Medium","High"),120,replace=TRUE))

as.character(f3)

输出

[1] "Medium" "High" "Medium" "High" "Medium" "High" "Low" "Low"

[9] "High" "Medium" "High" "Low" "High" "Medium" "Medium" "Low"

[17] "Medium" "Low" "High" "High" "Medium" "Medium" "Low" "Low"

[25] "High" "Low" "High" "Medium" "High" "Medium" "Low" "Low"

[33] "High" "High" "Low" "Low" "High" "Low" "Low" "High"

[41] "High" "Low" "High" "High" "Low" "High" "High" "Medium"

[49] "High" "Medium" "High" "Medium" "Low" "Medium" "Low" "Low"

[57] "High" "High" "High" "High" "Low" "Low" "High" "High"

[65] "Low" "Low" "High" "High" "High" "High" "Low" "Low"

[73] "Medium" "High" "Low" "Low" "Medium" "High" "Low" "Medium"

[81] "High" "Medium" "Medium" "Medium" "High" "Medium" "Low" "Medium"

[89] "Low" "High" "High" "Medium" "High" "Low" "Medium" "Low"

[97] "Low" "High" "Medium" "Low" "High" "Medium" "Low" "High"

[105] "High" "Low" "Medium" "Medium" "Medium" "Low" "High" "Low"

[113] "Medium" "High" "High" "High" "Medium" "Low" "Low" "Low"

示例 4

创建因子向量

让我们创建一个名为 f4 的向量,如下所示 -

f4<-factor(sample(c("First","Second","Third","Fourth","Fifth"),120,replace=TRUE))

f4

[1] Fifth Fifth First Second Fourth Fifth Second Second Fourth Second

[11] First Third Fifth Fourth Third First Third Third Third First

[21] Fourth Second Second Third Second Fourth Third Fourth First First

[31] Fifth Second Third Second Fourth Fourth Fourth Third Fourth First

[41] First Second Second Second Third Fifth Fifth Second Third Fifth

[51] Second Third Second First First Third Fifth Third Third Third

[61] Second Third Fifth Third Fifth Fifth Third Third Fourth First

[71] First Second Second Fourth Third Fifth Third First Third Third

[81] Fifth Fifth Third Third Fourth First First Fifth First Fifth

[91] Second Fifth Fourth First Fifth Fourth Second Third Second Fifth

[101] First Fourth Second Fourth Fifth Fifth Fifth Fourth Fourth Fourth

[111] Fourth First Fourth First Fourth Fourth First Fifth Second Third

Levels: Fifth First Fourth Second Third

打印没有水平的因子向量

使用as.character函数打印没有级别的因子向量 f4 -

f4<-factor(sample(c("First","Second","Third","Fourth","Fifth"),120,replace=TRUE))

as.character(f4)

输出

9] "Fourth" "Second" "First" "Third" "Fifth" "Fourth" "Third" "First"

[17] "Third" "Third" "Third" "First" "Fourth" "Second" "Second" "Third"

[25] "Second" "Fourth" "Third" "Fourth" "First" "First" "Fifth" "Second"

[33] "Third" "Second" "Fourth" "Fourth" "Fourth" "Third" "Fourth" "First"

[41] "First" "Second" "Second" "Second" "Third" "Fifth" "Fifth" "Second"

[49] "Third" "Fifth" "Second" "Third" "Second" "First" "First" "Third"

[57] "Fifth" "Third" "Third" "Third" "Second" "Third" "Fifth" "Third"

[65] "Fifth" "Fifth" "Third" "Third" "Fourth" "First" "First" "Second"

[73] "Second" "Fourth" "Third" "Fifth" "Third" "First" "Third" "Third"

[81] "Fifth" "Fifth" "Third" "Third" "Fourth" "First" "First" "Fifth"

[89] "First" "Fifth" "Second" "Fifth" "Fourth" "First" "Fifth" "Fourth"

[97] "Second" "Third" "Second" "Fifth" "First" "Fourth" "Second" "Fourth"

[105] "Fifth" "Fifth" "Fifth" "Fourth" "Fourth" "Fourth" "Fourth" "First"

[113] "Fourth" "First" "Fourth" "Fourth" "First" "Fifth" "Second" "Third"

以上是 如何在R中打印没有级别的因子向量? 的全部内容, 来源链接: utcz.com/z/343809.html

回到顶部