Simplify your R workflow with functions #rstats

February 24, 2013

(This article was first published on Strenge Jacke! » R, and kindly contributed to R-bloggers)

Update/ Thanks to Bernd I could improve the function of how to import the data, so here’s the updated script! /Update

In R, you often may have scripts or code snippets that will be reused. In such cases, you can write functions for your every-day-tasks. For instance, importing and converting data is such a task. I have written a small function importSPSS.R to do this:

importSPSS <- function(path) {
  data.spss <- read.spss(path,, use.value.labels=FALSE)
getValueLabels <- function(dat) {
  a <- lapply(dat, FUN = getValLabels)
  return (a)  
getValLabels <- function(x){
  rev(names(attr(x, "value.labels")))

This small function only gives little benefits regarding the saved typing effort. Referring to the code example under Migration, step 3: Importing (SPSS) variable and value labels, following things will change:

# Use "source" instead of "library"
# load data as data frame (function call)
myDat <- importSPSS("NWIN-Buch/GER_Services_FU_PV_dt.sav")
# copy all variable labels in separated list
myDat_vars <- attr(myDat, "variable.labels")
# copy all value labels as separated list (function call)
myDat_labels <- getValueLabels(myDat)

The benefit especially lies in getting access to value labels. Instead of

hist(myDat[,86], main=myDat_vars[86], labels=rev(attr(myDat_labels[[86]], "names")), breaks=c(0:4), ylim=c(0,400), xlab=NULL, ylab=NULL)

we can now write

hist(myDat[,86], main=myDat_vars[86], labels=myDat_labels[[86]], breaks=c(0:4), ylim=c(0,400), xlab=NULL, ylab=NULL)

so we don’t need to call the attr-function nor remember to reverse the label order for plotting.

Tagged: R, rstats, SPSS, Statistik

To leave a comment for the author, please follow the link and comment on their blog: Strenge Jacke! » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)