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

[ and [[ are a little bit faster (~15%) in the case below:
prefix <- sample(LETTERS, size=100, replace=TRUE)
paste(prefix, abs(100 * rnorm(100)), sep="-")
library(rbenchmark)
benchmark(sapply(strsplit(id, "-"), function(a) a[1]), sapply(strsplit(id, "-"), "[", 1), order="elapsed", replications=100)

The stupidest thing...

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:

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

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:

View original post

R for complex network analysis

(The page is still under construction.)

Complex network analysis is quite common in biology and social network analysis. However, CRAN task views don’t provide a specific section. Here, I collect R packages for complex network analysis.

iGraph: generic purpose

iGraph is a C library for manipulating graphs, complex network analysis, social network analysis. It has R and Python interfaces. My own experience is that it runs really fast.

Bioinformatics

Bits & Bites

This is my portal page of bits and pieces.

Packages & functions

  • base::unique(). More than one variables.
  • Remove NA. 1) any NA-related parameters, particularly in built-in function? 2) if not,  do something like d <- d[!is.na(d)]. Note: d != NA does NOT work! Read more.
  • Group data.frame into subgroups by column: split(). It may be really slow if the column is factor.
  • write lines to a text file, e.g. write Log file
  • File manipulation: file.exists(), file.create(), file.remove(), file.append(), file.append(), file.copy(), file.symlink(), file.link()
  • Save and load R objects
  • getopt package for parsing command parameters
  • XML package: reading, parsing, creating XML files
  • stringr package: make easier to work with strings, such as but not limited to proper dealing with NAs and zeros.
  • tm package: text mining framework
  • reshape2 package: restructure and aggregate data using just two functions: melt and cast.
  • Markdown: a format that enables easy authoring of reproducible web reports from R. In practice, knitr package first weaves R markdown (.rmd) files into plain markdown (.md) files, and then markdown package converts markdown files into HTML documents.
  • Sweave: weave Rnw files into TeX files that are then compiled into PDFs. The purpose is to create dynamic reports, which can be updated automatically if data or analysis change. In practice, you may use knitr (homepage), an R package that adds many new capabilities to Sweave.
  • Slidify: write slides in Rmd format. Example.
  • rCharts (R to JavaScript): a very powerful package to create, customize and publish interactive javascript visualizations from R using a familiar lattice style plotting interface. Tips: 1) generate time series chart by Richshaw, 2) Method html() gives the html code.
  • d3Network: create d3 JS directed network graphs.
  • RCurl: curl interface of R
  • print() – leading numbers, cat() – no leading numbers
  • Redirect console output to variable: capture.output() and sink()
  • file.path() constructs the path to a file from components in a platform-independent way.
  • foreach for parallel computation.
  • Circos like plot
  • OpenCPU

Digest articles

?help! Instant R search on Rdocumentation.org

Last week, I was working on an educational R project when I needed to consult the help files of different R packages and functions online. After doing some Google searches, it appeared to me that finding an easy-to-use tool was not as simple as I had expected. The closest that I got, were the websites Inside-R and R search, but as a user it wasn’t as “smooth” as what I was looking for. (I needed something really user-friendly for this educational project). Therefore, inspired by the documentation websites of programming languages/frameworks such as Ruby on Rails and AngularJS, I decided to build an online documentation search interface for R myself together with colleagues. Check the result on www.Rdocumentation.org!

Checking R documentation online instead of with the built-in R help function, can often provide some extra benefits. First, you are capable of searching through the latest version of…

View original post 278 more words