R语言因子序列的创建

2020年10月31日18:38:36R语言因子序列的创建已关闭评论

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为止)。

  • 版权声明:本篇文章(包括图片)来自网络,由程序自动采集,著作权(版权)归原作者所有,如有侵权联系我们删除,联系方式(QQ:452038415)。