[Rcpp-devel] Matrix columns

Dirk Eddelbuettel edd at debian.org
Sun May 15 21:22:19 CEST 2011


Nick,

On 15 May 2011 at 20:51, Nick Sabbe wrote:
| Hi Dirk. Thanks for your time and trouble.
| I agree there are simpler ways to achieve this functionality, but if I don't
| get around this, I won't be able to produce the thing I'm really after.

I fear that you think that because data.frame is a preferred structure in R,
it also must be in C++. That is not the case, and the reason is the internal
representation of a data.frame at the C (and hence C++) level.  You may be
better off decomposing your data.frame into columns or different types (in R
or C++) or just use a matrix if there is a common type.
 
| > Why is retMat on the right-hand side if you try to fill it?  Makes no real
| sense.
| 
| >From rcpp-quickref:
| // Reference the second column
| // Changes propagate to xx (same applies for Row)
| NumericMatrix::Column zzcol = xx( _, 1);
| zzcol = zzcol * 2;
| 
| Only difference in my code was that I was assigning from a NumericVector
| instead of from an altered version of the original NumericMatrix::Column. Or
| am I mistaken here? I've put it here again:
| NumericVector curcol = df(i);
| NumericMatrix::Column cl = retMat( _, i);
| cl = curcol;//from rcpp-quickref, I understand this alters the matrix?

I believe you to be mistaken. Twice assigning overwrites. Feel free to debug
and test to convince yourself.
 
| Then you appear to suggest that my approach using the "dim" attribute is no
| good, or at least you get the dimensions in a very different way (by using
| the data.frame as a list). I agree your way works, but why shouldn't mine?
| In any case, this sort of thing would be a valuable addition to Rcpp in my
| opinion.
| 
| Finally, you use:
| >+        V = DF[i];
| >+        M(_,i) = V;  // one way to assign using sugar operator _
| Which makes me wonder:
| * why DF[i] and not DF(i) (what's the difference - I still have not found a
| clear explanation)

It's only stylistic. You can use either one.

| * is M(_,i) = V; any different from NumericVector cl = M(_,i);cl=V; ? If so,
| how so?

As I recall, the former ("one-step") created a compiler error as the compiler
could not disambiguate some intermediate type. The second ("two-step")
worked, so I used that,  So the difference is that one works :)   [ In
theory, both should. Patches to make the former work are welcome. ]

| * thanks for letting me know _ is part of sugar. I may be able to figure out
| its use now, but isn't this worthy of a mention in rcpp-sugar.pdf?

As I said a few days ago, patches which enhance documentation or code are
always welcome and will always be properly credited (that is, if we agree
with them and incorporate them).

Dirk

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list