欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

R 语言 gl 示例说明

最编程 2024-03-19 12:01:39
...

gl()R语言中的函数用于通过指定其级别的模式来生成因子。

用法:
gl(x, k, length, labels, ordered)

参数:
x:级别数
k:重复次数
length:结果长度
labels:向量的标签(可选)
ordered:用于对级别进行排序的布尔值

范例1:


# R Program to generate factors
  
# Creating a factor
# using gl() function
x1 <- gl(2, 5)
  
# gl() function with
# length specified
x2 <- gl(3, 4, 12)
  
# Printing the factors
print(x1)
print(x2)

输出:

 [1] 1 1 1 1 1 2 2 2 2 2
Levels:1 2
 [1] 1 1 1 1 2 2 2 2 3 3 3 3
Levels:1 2 3

范例2:


# R Program to generate factors
  
# gl() function with
# length and labels specified
x1 <- gl(3, 4, 12, label = letters[1:12])
  
# gl() function with
# length, label and order specified
x2 <- gl(3, 4, 12, label = letters[1:12], ordered = T)
  
# Printing the factors
print(x1)
print(x2)

输出:

 [1] a a a a b b b b c c c c
Levels:a b c d e f g h i j k l
 [1] a a a a b b b b c c c c
Levels:a < b < c < d < e < f < g < h < i < j < k < l