Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中创建具有不同复制次数的因子_R - Fatal编程技术网

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

在R中创建具有不同复制次数的因子,r,R,下面的命令 region <- gl(6,2,24, label=c("ag", "cb", "cx", "ec", "hp", "mb")) 但是,当我尝试为不同数量的复制创建它时,它出错了。例如 当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

下面的命令

     region <- gl(6,2,24, label=c("ag", "cb", "cx", "ec", "hp", "mb"))
但是,当我尝试为不同数量的复制创建它时,它出错了。例如 当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?

region您需要的正是这个顺序吗?如果没有,这将起作用:

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

如果顺序很重要,那么调整代码应该很容易。

您需要按照正确的顺序吗?如果没有,这将起作用:

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

如果顺序很重要,调整代码应该很容易。

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

l
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
l我想如果我正确理解了您想要的输出,您可能需要使用
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
region我想如果我正确理解了您想要的输出,您可能需要使用
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

region我将使用
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"))

vector我将使用
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"))

vector+1这看起来比我的答案简单得多,我的答案与mroe fiddling做同样的事情+1这看起来比我的答案简单得多,我的答案与mroe fiddling做同样的事情