Christian Langkamp
2011-Nov-02 13:56 UTC
[R] Generate a sequence of vectors of different length
Hi everyone After the following setup sector=2 # Define Number of Sectors sectors=LETTERS[seq( from = 1, to = sector )] # Name sectors No_ent=round(3/runif(sector)) # Number of entities per sector #Tot_No_ent=sum(No_ent) Goal is to get a List like (A1, A2, A3, B1, B2, B3, B4) where A is denoting an industrial sector and then a numbered sequence of companies within the sector. The step I am missing is how to generate a sequence of vectors (one for each sector) with individual length being determined by No_ent. The goal is to generate a set of entities from different sectors. One simple way out of it would be to set the number of entities equal per sector and have a matrix, but I am quite sure it should also be possible for having a different number of entities in each sector. Once this is done, I can bind them together as vector with "as.vector(rbind(?))" (both as an (A,A,A,B,B,B,B) and (1,2,3,1,2,3,4) and then concatenate) Thanks, Christian Trials included the following bits A=for (i in 1:sector){ rep(i,No_ent[i]) } paste(LETTERS[i], seq(from =1, to =No_ent[i]), sep = "") but I don''t get the correct object definition right. [[alternative HTML version deleted]]
Dimitris Rizopoulos
2011-Nov-02 14:07 UTC
[R] Generate a sequence of vectors of different length
One approach is: sectors <- 2 namSec <- LETTERS[seq_len(sectors)] nSec <- round(3 / runif(sectors)) mapply(paste, namSec, sapply(nSec, seq_len), MoreArgs = list(sep = "")) I hope it helps. Best, Dimitris On 11/2/2011 2:56 PM, Christian Langkamp wrote:> Hi everyone > After the following setup > > sector=2 # Define Number of Sectors > > sectors=LETTERS[seq( from = 1, to = sector )] # Name sectors > > No_ent=round(3/runif(sector)) # Number of entities per sector > > #Tot_No_ent=sum(No_ent) > > > > Goal is to get a List like > > (A1, A2, A3, B1, B2, B3, B4) where A is denoting an industrial sector and > then a numbered sequence of companies within the sector. > > > > The step I am missing is how to generate a sequence of vectors (one for each > sector) with individual length being determined by No_ent. > > The goal is to generate a set of entities from different sectors. One simple > way out of it would be to set the number of entities equal per sector and > have a matrix, but I am quite sure it should also be possible for having a > different number of entities in each sector. > > > > Once this is done, I can bind them together as vector with > "as.vector(rbind(?))" (both as an (A,A,A,B,B,B,B) and (1,2,3,1,2,3,4) and > then concatenate) > > > > Thanks, Christian > > > > > > Trials included the following bits > > A=for (i in 1:sector){ > > rep(i,No_ent[i]) > > } > > paste(LETTERS[i], seq(from =1, to =No_ent[i]), sep = "") > > but I don't get the correct object definition right. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Apparently Analagous Threads
- Standard introductory presentation
- R table as integrable object for large Latex Documents - avoiding SWeave
- LaTeX consistent publication graphics from R and Comparison of GLE and R
- Subtract matrices within arrays along indices
- Re place Values within vector using Translation vector