“[” with the apply() functions, revisited
R-bloggers 2014-04-30
(This article was first published on The stupidest thing... » R, and kindly contributed to R-bloggers)
I’d mentioned in the fall that one could use "["
in the apply
-type functions, like this:
id <- c("ZYY-43S-CWA3", "6YU-F4B-VD2I")sapply(strsplit(id, "-"), "[", 2)
I just realized that you can use this with matrices, too. If you have a list of matrices, you can pull out rows and columns with this technique.
z <- list(matrix(1:10, nrow=2), matrix(11:20, nrow=2))lapply(z, "[", 1, )lapply(z, "[", , 3)
As you can see, my data isn’t “tidy.”
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...