“[” and “[[” with the apply() functions

August 20, 2013
By

(This article was first published on The stupidest thing... » R, and kindly contributed to R-bloggers)

Did you know you can use "[" and "[[" as function names for subsetting with calls to the apply-type functions?

For example, suppose you have a bunch of identifier strings like "ZYY-43S-CWA3" and you want to pull off the bit before the first hyphen ("ZYY" in this case). (For code to create random IDs like that, see the end of this post.)

Suppose the IDs are in a vector of character strings, id.

If I wanted to grab the bit before the first hyphen, I would typically use strsplit and then sapply with function(a) a[1], as so:

sapply(strsplit(id, "-"), function(a) a[1])

But in place of function(a) a[1], you can use "[", 1, as follows:

sapply(strsplit(id, "-"), "[", 1)

I think that's kind of cute. You can use "[[" the same way, if you're working with lists.

Here's some code to create random IDs of this form, to test out the above:

nind <- 8
lengths <- c(3, 3, 4)
id <- NULL
for(i in seq(along=lengths)) {
  randchar <- sample(c(LETTERS, 0:9), nind*lengths[i], replace=TRUE)
  randstring <- apply(matrix(randchar, ncol=lengths[i]),
                   1, paste, collapse="")
  if(is.null(id)) id <- randstring
  else id <- paste(id, randstring, sep="-")
}

To leave a comment for the author, please follow the link and comment on his blog: The stupidest thing... » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.