[Rcpp-devel] Joining each row of CharacterMatrix to return a CharacterVector?

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Dec 11 00:19:40 CET 2012


On Mon, Dec 10, 2012 at 5:43 PM,  <hickey at wehi.edu.au> wrote:
> I preface this by stating that I'm very much a Rcpp beginner who is
> comfortable in R but I've never before used C++. I'm working through the
> Rcpp documentation but haven't been able to answer my question.
> I've written an Rcpp (v0.10.1) function f that takes as input a
> CharacterMatrix X. X has 20 million rows and 100 columns. For each row of X
> the function alters certain entries of that row according to rules governed
> by some other input variables. f returns the updated version of X. This
> function works as I'd like it to:
> # a toy example with nrow = 2, ncol = 2
>> X <- matrix('A', ncol = 2, nrow = 2)
>> X
>      [,1] [,2]
> [1,] "A"  "A"
> [2,] "A"  "A"
>> X <- f(X, other_input_variables)
>> X
>      [,1] [,2]
> [1,] "Z"  "A"
> [2,] "z"  "A"
> However, instead of f returning a CharacterMatrix as it currently does, I'd
> like to return a CharacterVector Y, where each element of Y is a "collapsed"
> row of the updated X.
> I can achieve the desired result in R by using:
> Y <- apply(X=X, MARGIN = 1, FUN = function(x){paste0(x, collapse = '')})
>> Y
> [1] "ZA" "zA"

You can do it more (speed) efficiently in R, too, if memory is no
object, since you can just R-loop over the far fewer columns:

R> X <- matrix(c("Z", "z", "A", "A"), nrow=2)
R> Y <- do.call(paste0, lapply(1:ncol(X), function(i) X[,i]))
R> Y
[1] "ZA" "zA"

but doing it in C(++) will definitely be more memory efficient, and
likely speed efficient, too, so it will be a good exercise, and for
that Dirk has given you a good head start :-)


Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the Rcpp-devel mailing list