The magic empty bracket

I have been working with R for some time now, but once in a while, basic functions catch my eye that I was not aware of…
For some project I wanted to transform a correlation matrix into a covariance matrix. Now, since cor2cov does not exist, I thought about “reversing” the cov2cor function (stats:::cov2cor).
Inside the code of this function, a specific line jumped into my retina:

r[] <- Is * V * rep(Is, each = p)

What’s this [ ]?

Well, it stands for every element E_{ij} of matrix E. Consider this:

mat <- matrix(NA, nrow = 5, ncol = 5)
> mat
     [,1] [,2] [,3] [,4] [,5]
[1,]   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA
[3,]   NA   NA   NA   NA   NA
[4,]   NA   NA   NA   NA   NA
[5,]   NA   NA   NA   NA   NA

With the empty bracket, we can now substitute ALL values by a new value:

mat[] <- 1
> mat
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
[4,]    1    1    1    1    1
[5,]    1    1    1    1    1

Interestingly, this also works with lists:

L <- list(a = 1, b = 2, c = 3)

[1] 1

[1] 2

[1] 3

L[] <- 5
> L
[1] 5

[1] 5

[1] 5




13 Responses to The magic empty bracket

  1. Michael Folkes says:

    awesome thanks! one good turn deserves another.
    In case you didn’t resolve cor2cov, here’s an eg:

    # this code shows how to build correlated series, which in this case have lognormal error
    # the example represents four time series (so the matrices are 4×4)


    # correlation of 50% between series
    cor.mat<- matrix(c(1,0.5,0.5,0.5,

    # next two lines are cor2cov():
    d <- sqrt(diag(cov.mat))
    cov.mat2 <- outer(d, d)*cor.mat


    #demonstrating the math is correct:


    # demonstrating the sampled results have desired correlation:

  2. eran says:

    Great tip, will come in handy (a.s.)

    Maybe drop a line to Norman S. Matloff, he can add it to the next addition of “the art of R programming”.

  3. carlwitthoft says:

    To make this post complete, you might want to discuss what happens to “r” when you type
    > r r[] <- something

  4. carlwitthoft says:

    OK, let me try that again. WordPress is destroying lines of text near the character “>” so pretend the prompt is “%”
    % r <- something
    % r[] <- something

    • anspiess says:

      r[] <- 5 will fill each element of an existing object with 5 while r <- something, if r already exists, will results in a new object r with a single value 5.
      What would be really nice as a syntax:
      r[] <- 5 * r[]
      doing an element-wise function as a loop instead of lapply.
      Should be possible, since lists are stored internally as C objects, not?


  5. Ken Butler says:

    Isn’t this just the same thing as r[,]<-5, which (to my mind) more transparently sets all the elements in all the rows and all the columns to 5?

  6. Reblogged this on Stats in the Wild and commented:
    I didn’t know this either!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: