建立R中的向量列表

时间:2014-02-21 13:20:55

标签: r list

我想定义一个向量列表。向量长度为​​4,列表长度为N.

我正在尝试

list A=as.list(rep(c("","","",""),length=N)

但我得到输出

[[1]]
[1] ""

[[2]]
[1] ""

[[3]]
[1] ""

但我需要输出为

[[1]]
[1] "" "" "" ""

[[2]]
[1] "" "" "" ""

[[3]]
[1] "" "" "" ""

怎么可以这样做?

由于

2 个答案:

答案 0 :(得分:1)

N<-10
lapply(1:N,function(x)rep(c("","","",""),N))

实际上如果你没有重复N次(即所有项目都相同),你可能需要:

lapply(1:N,function(x)c("","","",""))

答案 1 :(得分:1)

这与特洛伊相似,但我觉得值得张贴的不同之处在于:

 replicate(N, character(4), s=F)

使用N==3

[[1]]
[1] "" "" "" ""

[[2]]
[1] "" "" "" ""

[[3]]
[1] "" "" "" ""