[R] strings concatenation and organization (fast)

Phil Spector spector at stat.berkeley.edu
Fri Jun 15 22:40:26 CEST 2012


Ben -
    There are most likely faster ways, but

matrix(apply(matrix(rep_vec,ncol=3,byrow=TRUE),1,paste,collapse=''),
        ncol=4,byrow=TRUE,dimnames=list(NULL,nms))

seems reasonably fast.   (Do you really mean 4 columns and *5* rows?
With rep_vec = rep(vec,times=20), I get 25 rows.)

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Fri, 15 Jun 2012, Ben quant wrote:

> Hello,
>
> What is the fastest way to do this? I has to be done quite a few times.
> Basically I have sets of 3 numbers (as characters) and sets of 3 dashes and
> I have to store them in named columns. The order of the sets and the column
> name they fall under is important. The actual numbers and the pattern/order
> of the sets should be considered random/unpredictable.
>
> Sample data:
> vec = c("1","2","3","-","-","-","4","5","6","1","2","3","-","-","-")
> rep_vec = rep(vec,times=20)
> nms = c("A","B","C","D")
>
> I need to get this:
>  A     B     C     D
> "123" "---" "456" "123"
> "---" "123" "---" "456"
> "123" "---" "123" "---"
> "456" "123" "---" "123"
> "---" "456" "123" "---"
>
> Note: a matrix of 4 columns and 5 rows of concatenated string sets.
>
> Thanks!!
>
> Ben
>
> 	[[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.
>



More information about the R-help mailing list