Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

#-----------------------------------
# AAK: Sat 15 Aug 2014 00:48:44 PDT
# Loop operators in R
#-----------------------------------
x <- list(a=1:10, b=rnorm(20))
#Applies the function mean on each element of the list:
y <- lapply(x,mean)

x <- 1:4
y <- lapply(x,runif)
# Passing arguments to runif
y <- lapply(x,runif,min=0,max=10)

# sapply is the same as lapply but the result is simplified (in its type)

x <- matrix(rnorm(200),20,10)
# returns the mean of the columns:
y <- apply(x,2,mean)
# returns the sum of the rows:
y <- apply(x,1,sum)
# in apply(Matrix,I,FUNC), I is the index that is preserved

# mapply is multivariable apply,

noise <-function(n,mean,std){
rnorm(n,mean,std)
}

# Imagine we want to create all the combination of
# n=(1,2,3,4,5) and means=(1,1.5,2,2.5,3)

y <- mapply(noise,1:5,seq(1,3.0,0.5),1.0)

# tapply applies the function on portions of a vector
x <- c(rnorm(100), runif(100), rnorm(100,2))
# Creates a factor with 3 levels  1x100, 2x100, 3x100
indx <- gl(3,100)
means <- tapply(x,indx,mean)
# returns the mean of each group (levels) i.e here 3 numbers:
print(means)

# Using factor you can also split a vector:

y <- split(x,indx)
print(summary(y))

library(datasets)

# Example using airquality dataset, splitting data frame:

s <- split(airquality, airquality$Month) # Note the on the fly function definition: res <- sapply(s, function(x) colMeans(x[,c("Ozone", "Solar.R","Wind")], na.rm=TRUE)) print(res) # Example using data frames: library(datasets) # See mtcars data frame # Returns the average of car's mpg for each cyl group (4, 5, 6 cyldender) cylmean <- tapply(mtcars$mpg,mtcars\$cyl,mean)
print(cylmean)


last update: Wed Aug 19, 2015