在R中创建具有不同重复次数的因子

时间:2013-05-02 11:29:31

标签: r

以下命令

     region <- gl(6,2,24, label=c("ag", "cb", "cx", "ec", "hp", "mb"))

以下列方式创建一个因子

     structure(c(1L, 1L, 2L, 2L, 2L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 1L, 
      1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("ag", 
     "cb", "cx", "ec", "hp", "mb"), class = "factor")

但是当我尝试为不同数量的重复创建它时,它就出错了。例如 当ag和cb各自重复三次时,我需要这样的东西

     structure(c(1L, 1L,1L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 1L, 
      1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("ag", 
     "cb", "cx", "ec", "hp", "mb"), class = "factor")

如何编写命令

       region <- gl(6,2,24, label=c("ag", "cb", "cx", "ec", "hp", "mb")) now?

4 个答案:

答案 0 :(得分:1)

gl只是rep.int的包装器。你可以自己打电话给代表

l <- c("ag", "cb", "cx", "ec", "hp", "mb")
# I will presume you want the output to now be length 28 to account
# for the extra replications in the first two levels
 factor(rep_len(rep.int(l, times = rep.int(c(3,2), c(2,4))),28))
 ## [1] ag ag ag cb cb cb cx cx ec ec hp hp mb mb 
 ## [14] ag ag ag cb cb cb cx cx ec ec hp hp mb mb
 ## Levels: ag cb cx ec hp mb

答案 1 :(得分:0)

您需要按照确切的顺序吗?如果没有,这将有效:

factor(rep(c('ag', 'cb', 'cx', 'ex', 'hp', 'mb'), times=c(5, 6, 3, 4, 4, 4)))

如果订单很重要,那么适应代码应该很容易。

答案 2 :(得分:0)

如果我正确理解了你想要的输出,你可能需要做一些手动摆弄rep。这就是gl用来制作因素的原因:

region <- rep( c( rep( c( "ag" , "cb" ) , each = 3 )  , rep ( c( "cx", "ec", "hp", "mb" ) , each = 2 ) ) , times = 2 )
region <- as.factor( region )
region
# [1] ag ag ag cb cb cb cx cx ec ec hp hp mb mb ag ag ag cb cb cb cx cx ec ec hp hp mb mb
# Levels: ag cb cx ec hp mb

答案 3 :(得分:0)

我会使用rep创建一个带有正确重复的数字向量,然后通过指定标签将其转换为一个因子。

vector <- c(rep(1:2, each=3), rep(3:6, each=2))

region <- factor(vector,
                 levels=1:6,
                 labels=c("ag", "cb", "cx", "ec", "hp", "mb"))