- A+
所属分类:R语言
R语言因子序列的创建
使用gl函数可以方便地创建一个因子向量。其基本形式为:
gl (n, k, length = n*k, labels = 1:n, ordered = FALSE )
相应的示例代码如下:
gl(3,4,labels=c("one","two","three")) [1] one one one one two two two two three three three three Levels: one two three gl(3,2,length=12,labels=c("one","two","three")) [1] one one two two three three one one two two three three Levels: one two three
在上例中,gl(3,4,labels=c("one","two","three"))可以理解为:产生一个因子水平为"one","two","three"的无序因子向量,其中每个水平连续重复4次。
gl(3,2,12,labels=c("one","two","three"))则可以理解为:每个水平连续重复了两次产生“one one two two three three”,而后该序列又重复了一次(直至向量长度为12为止)。