Summer 2010 — R: Data Goals

Summer 2010 — R: Functions

<- R: Summarization and Aggregation Home R: Reshaping ->

Contents

  1. Intro
  2. function()
  3. Argument Interpretation
  4. Defaults

Intro

If you have a series of commands that you intend to run many times on many pieces of similar data (say, in combination with the plyr package), then you will want to write a function.

function()

You define a new function with the function() command. Its arguments are the arguments you want to pass to your function.

foo <- function(x){
  x <- x*10
  return(x)
}
				

Run foo(1:10) and see what you get.

Here are some more useful functions, of the sort you're more likely to write.

zscore <- function(x){
  ## Returns z-scored values
  x.mean <- mean(x)
  x.sd <- sd(x)
	
  x.z <- (x-x.mean)/x.sd
	
  return(x.z)
}
				
x <- rnorm(100, mean = -1)

mean(x)
mean(zscore(x))
zscoreByGroup <- function(x, groups){
  #Compute zscores within groups
  out <- rep(NA, length(x))
  
  for(i in unique(groups)){
    out[groups == i] <- zscore(x[groups == i])
  }
  return(out)
}
				
x <- c(rnorm(100, mean = -1), rnorm(100, mean = 1))
groups <- c(rep("A",100), rep("B",100))

tapply(x, groups, mean)
tapply(zscore(x), groups, mean)
tapply(zscoreByGroup(x, groups), groups, mean)

Argument Interpretation

R is pretty clever in its interpretation of arguments passed to functions. By default, it will assume that the first argument matches the first defined argment, and the second the second, etc.

bar <- function(a,b,c){
  return(c(a = a, b = b, c = c))
}
				
bar(1,2,3)
bar(4,1,9)

You can also explicitly declare which values get passed to which arguments.

bar(a = 1, c = 3, b = 2)
bar(c = 3, b = 2, a = 1)

Defaults

You can also define default values for arguments.

bar <- function(a=1,b=2,c=3){
  return(c(a = a, b = b, c = c))
}
				
bar()
bar(5)
bar(b = 4)