Subset views in R

August 28, 2010

(This article was first published on Struggling Through Problems » R, and kindly contributed to R-bloggers)

I don’t know how to do this in R. So let me just say why I can’t.

I wanted something akin to Boost‘s sub-matrix views, where you can have indexes map back to the original matrix, so you don’t create a new object.

Sounds straightforward, just overload ‘[[‘ to subtract the offset and check the length. Alas, no dice. R zealously copies objects to the point this is not (as far as I know, which isn’t much) possible.

To demonstrate, the following function executes and times expressions operating on a vector called “M”.

time.op = function(N, Exp) {
        Exp = parse(text=Exp)
        M = numeric(N)

        N.Trials = 10
        Times = numeric(N.Trials)

        for (II in 1:N.Trials) {
                Times[[II]] = system.time(eval(Exp))[['elapsed']]



> time.op(1e5, 'sqrt(M)')
[1] 0.0024

Then see, does the size of M affect the time of the operation?

time.test = function(Exp) {
        Ns = 10^(1:6)
        Times = sapply(Ns, time.op, Exp=Exp)

        data.frame(Ns, Times)
> time.test('sqrt(M)')
     Ns  Times
1 1e+01 0.0000
2 1e+02 0.0000
3 1e+03 0.0001
4 1e+04 0.0004
5 1e+05 0.0027
6 1e+06 0.0274


And here’s why we know it’s copying:

> time.test('list(M)')
     Ns  Times
1 1e+01 0.0001
2 1e+02 0.0001
3 1e+03 0.0002
4 1e+04 0.0000
5 1e+05 0.0004
6 1e+06 0.0086

Or with attributes

> time.test('attr(M, "name") = "mike"')
     Ns  Times
1 1e+01 0.0000
2 1e+02 0.0000
3 1e+03 0.0000
4 1e+04 0.0000
5 1e+05 0.0006
6 1e+06 0.0081

Good luck making a subset without copying!

And here’s the relevant parts of the R code.

Making a list (main/builtin.c)

    for (i = 0; i < n; i++) {
                if (TAG(args) != R_NilValue) {
                    SET_STRING_ELT(names, i, PRINTNAME(TAG(args)));
                    havenames = 1;
                else {
                    SET_STRING_ELT(names, i, R_BlankString);
                if (NAMED(CAR(args)))
                    SET_VECTOR_ELT(list, i, duplicate(CAR(args)));
                    SET_VECTOR_ELT(list, i, CAR(args));
                args = CDR(args);
            if (havenames) {
                setAttrib(list, R_NamesSymbol, names);

Note the repeated calls to “duplicate”.

And yes, duplicate does copy, and it is deep (main/duplicate.c):

case VECSXP:
        n = LENGTH(s);
        PROTECT(t = allocVector(TYPEOF(s), n));
        for(i = 0 ; i < n ; i++)
            SET_VECTOR_ELT(t, i, duplicate1(VECTOR_ELT(s, i)));
        DUPLICATE_ATTRIB(t, s);

To leave a comment for the author, please follow the link and comment on their blog: Struggling Through Problems » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)