Split Function

split is NOT a loop function. But it works well with other loop functions.

Simple split

> x <- c(rnorm(10), runif(10), rnorm(10, 1))
> f <- gl(3, 10)

> split(x, f)
$`1`
 [1]  0.6546703  0.1908048  0.7510316 -0.5275388  0.1565112 -1.0819140  0.5881377  1.9318782 -1.6296265  0.1217391

$`2`
 [1] 0.8565242 0.9489818 0.6900339 0.9405762 0.5868754 0.7953035 0.1449435 0.3803465 0.6014722 0.7375775

$`3`
 [1]  0.9127986 -0.1662831  0.4377588  0.9954957  1.0922615  1.2733158  1.4715993  3.6705109  1.1215348  1.1913898

Split by month

It works well with more compex datasets. Lets split dataset by month. We will uset the following dataset.

> library(datasets)
> head(airquality)
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

Now we split the dataset by month and calculate average wind and temprature.

> s <- split(airquality, airquality$Month)
> lapply(s, function(x) colMeans(x[, c("Wind", "Temp")], na.rm=TRUE))
$`5`
    Wind     Temp
11.62258 65.54839

$`6`
    Wind     Temp
10.26667 79.10000

$`7`
     Wind      Temp
 8.941935 83.903226

$`8`
     Wind      Temp
 8.793548 83.967742

$`9`
 Wind  Temp
10.18 76.90

Last updated