[R] strings concatenation and organization (fast)

Rui Barradas ruipbarradas at sapo.pt
Sat Jun 16 00:17:41 CEST 2012


Hello,

I didn't know rep_vec couldn't be modified, I thought vec was the main 
vector.
Revised.

vec = c("1","2","3","-","-","-","4","5","6","1","2","3","-","-","-")
rep_vec = rep(vec,times=20)
nms = c("A","B","C","D")
rv <- sapply(split(rep_vec, cumsum(rep(c(1, 0, 0), length(rep_vec)/3))), 
paste, collapse="")
mat <- matrix(rv, ncol=length(nms), byrow=TRUE, dimnames=list(NULL, nms))
mat

Rui Barradas

Em 15-06-2012 22:36, Ben quant escreveu:
> I'm checking out Phil's solution...so far so good.  Thanks! Yes, 25 not
> 5 rows, sorry about that.
>
> Rui - I can't modify rep_vec...that's just sample data. I have to start
> with rep_vec and go from there.
>
> have a good weekend all...
>
> Ben
>
> On Fri, Jun 15, 2012 at 2:51 PM, Rui Barradas <ruipbarradas at sapo.pt
> <mailto:ruipbarradas at sapo.pt>> wrote:
>
>     Hello,
>
>     Try
>
>
>
>     vec = c("1","2","3","-","-","-","4",__"5","6","1","2","3","-","-","-__")
>     nms = c("A","B","C","D")
>     rep_vec <- rep(sapply(split(vec, cumsum(rep(c(1, 0, 0), 5))), paste,
>     collapse=""), 4)
>     mat <- matrix(rep_vec, nrow=5, byrow=TRUE, dimnames=list(NULL,nms))
>     mat
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Em 15-06-2012 21:11, Ben quant escreveu:
>
>         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 <mailto:R-help at r-project.org> mailing list
>         https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
>
>
>



More information about the R-help mailing list