Site icon R-bloggers

Simplify your R workflow with functions #rstats

[This article was first published on Strenge Jacke! » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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) {
  require("foreign")
  data.spss <- read.spss(path, to.data.frame=TRUE, use.value.labels=FALSE)
  return(data.spss)
}
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"
source("lib/importSPSS.R")
# 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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.