<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Thanks very much, Dirk and Steve. <div><br></div><div>Always slightly fear-inducing when someone starts their reply with "Ah, the joy of working with X" :) I'll have a go at implementing your suggestion on my two examples, Dirk. <div><br></div><div>I think learning more about Rcpp will become my Christmas-holiday project. It's already saved me buckets of computational time in this past week and that's without even really knowing what I'm doing :)</div><div>Pete<br><div><div>On 11/12/2012, at 10:09 AM, Dirk Eddelbuettel wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><br>Hi Pete,<br><br>On 11 December 2012 at 09:43, <a href="mailto:hickey@wehi.EDU.AU">hickey@wehi.EDU.AU</a> wrote:<br>| I preface this by stating that I'm very much a Rcpp beginner who is comfortable<br>| in R but I've never before used C++. I'm working through the Rcpp documentation<br>| but haven't been able to answer my question.<br>| <br>| I've written an Rcpp (v0.10.1) function f that takes as input a CharacterMatrix<br>| X. X has 20 million rows and 100 columns. For each row of X the function alters<br>| certain entries of that row according to rules governed by some other input<br>| variables. f returns the updated version of X. This function works as I'd like<br>| it to: <br>| # a toy example with nrow = 2, ncol = 2<br>| > X <- matrix('A', ncol = 2, nrow = 2)<br>| > X<br>|      [,1] [,2]<br>| [1,] "A"  "A" <br>| [2,] "A"  "A" <br>| > X <- f(X, other_input_variables)<br>| > X<br>|      [,1] [,2]<br>| [1,] "Z"  "A" <br>| [2,] "z"  "A" <br>| <br>| However, instead of f returning a CharacterMatrix as it currently does, I'd<br>| like to return a CharacterVector Y, where each element of Y is a "collapsed"<br>| row of the updated X.<br>| <br>| I can achieve the desired result in R by using: <br>| Y <- apply(X=X, MARGIN = 1, FUN = function(x){paste0(x, collapse = '')}) <br>| > Y<br>| [1] "ZA" "zA"<br>| <br>| but I wondered whether this "joining" is likely to be more efficiently<br>| performed within my function f? If so, how do I join the 100 individual<br>| character entries of a row of the CharacterMatrix X into a single string that<br>| will then comprise an element of the returned CharacterVector Y?<br><br>Ah, the joy of working with character strings/vectors/pointers :)  <br><br>You certainly can. And there will be a lot of old, bad, ... tutorials out<br>there.  I can't right now think of a good tutorial to point you to -- other<br>than the perennial "C++ Annotations" by Brokken which is at the same time<br>good, current, up-to-date and free (!!) -- so maybe you shoud continue with<br>the little 2 x 2 and 3 x 3 examples:<br><br> i)   loop over a row, first init the target string to be ""<br> ii)  assign each element of the matrix to a string<br> iii) append, which can be as easy as using the   +   for two strings<br> iv)  accumulate the result strings in a vector of strings<br><br>That should work, does not require pointers, free, malloc, ...  You can<br>optimize later.<br><br>Hope this helps,  Dirk<br><br>-- <br>Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com">http://dirk.eddelbuettel.com</a>  <br></div></blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" style="font: normal normal normal 12px/normal Helvetica; ">--------------------------------</font></div></div><div>Peter Hickey,</div><div>PhD Student/Research Assistant,</div><div>Bioinformatics Division,</div><div>Walter and Eliza Hall Institute of Medical Research,</div><div>1G Royal Parade, Parkville, Vic 3052, Australia.</div><div>Ph: +613 9345 2324</div><div><br></div><div><a href="mailto:hickey@wehi.edu.au">hickey@wehi.edu.au</a></div><div><a href="http://www.wehi.edu.au">http://www.wehi.edu.au</a></div></span></div></span></div></span></div></span></div></span></span>
</div>
<br></div></div><br clear="both">
______________________________________________________________________<BR>
The information in this email is confidential and intended solely for the addressee.<BR>
You must not disclose, forward, print or use it without the permission of the sender.<BR>
______________________________________________________________________<BR>
</body></html>