user3171906
user3171906

Reputation: 583

building list of vectors in R

I want to define a list of vectors. The vector length is 4 and the length of list is N.

I am trying

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

but I am getting output

[[1]]
[1] ""

[[2]]
[1] ""

[[3]]
[1] ""

But I need the output as

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

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

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

How could this be done?

Thanks

Upvotes: 0

Views: 57

Answers (2)

BrodieG
BrodieG

Reputation: 52687

This is similar to Troy's, but different enough that I think it's worth posting:

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

With N==3:

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

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

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

Upvotes: 1

Troy
Troy

Reputation: 8701

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

actually if you're not repeating N times (i.e. all items the same), you probably need:

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

Upvotes: 1

Related Questions