Introduction to R Programming videos

Get a thorough introduction to programming in R through 21 videos:

The full set of videos can be easily browsed in this Google+ post.

To remove any confusion: I work at YouTube, and I developed the videos as part of a 20% project. Also notice that the videos are available on YouTube under a Creative Commons license.

Thank you to the Google Developers team!

Best,
David

Five functions I wished I had known five years ago

Like just about every other grad statistics student, my first 2-3 years of education in R programming was a bit disjoint. I’d pick up skills and learn new functions from random courses and sources. This post features five functions that I wish I’d known soon after I started programming in R but didn’t learn until 2 or more years in.

identical(x, y). Check if two R objects x and y are exactly identical.

txtProgressBar, setTxtProgressBar. Set the parameters of a progress bar using txtProgressBar, then print the progress bar in each iteration of a loop using setTxtProgressBar. Pretty much any for loop that takes more than five seconds to run would benefit from the use of these functions.

package.skeleton. Generate all the skeleton files for a package using the objects in an R session. I’ve taken to creating a package for most projects I work on to organize data and functions. If you aren’t familiar with package building, start by viewing this video guide.

by(data, INDICES, FUN, …). Apply a function across subsets of data, where the subsets are defined by the INDICES argument. The by function returns an object that summarizes each particular subset of the data according to the indices used.

traceback. If there’s an error returned from a function, use the traceback function to trace the last command run that caused the error.

Three honorable mentions: browser, rgb, and how to clear the console screen.

Consider leaving your favorites in the comments!

rasterImage

rasterImage(image, xleft, ybottom, xright, ytop, angle=0, interpolate=TRUE)
Create a grid of colors, which could represent pixels of an image or something like a correlation matrix.
  • image – A "raster" object, or an object that can be coerced to a raster object via as.raster (e.g. an array with dimensions d x d x 3).
  • xleft, ybottom, xright, ytop – The boundaries of the raster image.
  • angle – If desired, specify an angle to rotate the raster image. The rotation pivots on the bottom-left corner.
  • interpolate – If TRUE, then automatic smoothing will be done on the raster image.

Continue reading

readPNG (png package)

readPNG(source)
Generate a matrix for a raster image from a PNG image. This is especially interesting since not only can this raster image be added to a plot, but the red-green-blue make-up (see rgb) of each pixel may be viewed in the output of readPNG. It is worth noting that functionality on the Windows platform may require some fussing (see the readPNG help file).
  • source – The file name of the PNG file as a character string. If necessary, include the path to the file.

Continue reading

findFn (sos package)

findFn(string, maxPages = 20)
Do a search for functions, which opens an interactive HTML page of results. The results may be sorted in a variety of ways and also link to help files for each function. The results page will automatically open in whatever application has been designated to open .html files (usually a browser is the default).
  • string – A character string to search.
  • maxPages – Maximum number of pages to return, i.e. number of links is 20*maxPages.

Continue reading