R apply Function


apply() function applies a function to margins of an array or matrix.

apply(x,margin,func, ...)

• x: array
• margin: subscripts, for matrix, 1 for row, 2 for column
• func: the function
...

>BOD    #R built-in dataset, Biochemical Oxygen Demand
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

Sum up for each row:
> apply(BOD,1,sum)
[1]  9.3 12.3 22.0 20.0 20.6 26.8

Sum up for each column:
> apply(BOD,2,sum)
  Time demand 
    22     89 

Multipy all values by 10:
> apply(BOD,1:2,function(x) 10 * x)
     Time demand
[1,]   10     83
[2,]   20    103
[3,]   30    190
[4,]   40    160
[5,]   50    156
[6,]   70    198

Used for array, margin set to 1:
> x <- array(1:9)
> apply(x,1,function(x) x * 10)
[1] 10 20 30 40 50 60 70 80 90

Two dimension array, margin can be 1 or 2:
> x <- array(1:9,c(3,3))
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> apply(x,1,function(x) x * 10) #or apply(x,2,function(x) x * 10)
[1] 10 20 30 40 50 60 70 80 90

lapply() function can handle data frame with similar results, return is a list:
> lapply(BOD,sum)
$Time
[1] 22

$demand
[1] 89

> lapply(BOD,mean)
$Time
[1] 3.666667

$demand
[1] 14.83333

sapply() has similar function, it defines "simplify=TRUE" by default, thus return a vector:
> sapply(BOD,sum)
  Time demand 
    22     89 
> sapply(BOD,sum,simplify=FALSE)
$Time
[1] 22

$demand
[1] 89