VARIOUS LOOPING TECHNIQUES in R with EXAMPLES

 

VARIOUS LOOPING TECHNIQUES in R with EXAMPLES

In this text, we are going to over some of the most extraordinary functions to help us loop in R. Mastering these tools (functions) would make a more efficient R-programmer. I use R for various geologic/geochemical and hydrogeologic investigations and I use the functions discussed here a lot. Although, the examples used in this write up do not produce any graphs, you can easily produce graphs with the looping functions in R.

lapply(X, FUN, …)

sapply(X, FUN, …,
simplify = TRUE, USE.NAMES = TRUE)

vapply(X, FUN, FUN.VALUE,
…, USE.NAMES = TRUE)

replicate(n, expr,
simplify = “array”)

simplify2array(x, higher
= TRUE)

lapply

Apply a Function over a
List or Vector. loop over a list, item by item. Actual looping is done
in C language.

str(lapply)
## function (X, FUN, ...)
object=list(a=1:10, b=rnorm(10))
object
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $b
##  [1] -0.45736 -1.00367  1.61543  0.12046 -0.03302 -0.49324 -0.74551
##  [8]  0.85625 -0.81216 -0.18206
lapply(object, sd)
## $a
## [1] 3.028
## 
## $b
## [1] 0.8123

lapply returns a list of
the same length as X, each element of which is the result of applying
FUN to the corresponding element of X.

object=1:10
object
##  [1]  1  2  3  4  5  6  7  8  9 10
lapply(object, runif, min=0, max=10) #runif generates random deviates.
## [[1]]
## [1] 8.837
## 
## [[2]]
## [1] 8.957 3.166
## 
## [[3]]
## [1] 4.177 3.054 2.045
## 
## [[4]]
## [1] 8.055 7.978 7.803 2.051
## 
## [[5]]
## [1] 5.077 4.916 1.499 8.946 5.967
## 
## [[6]]
## [1] 3.674 1.465 3.482 2.330 7.211 4.193
## 
## [[7]]
## [1] 3.925 2.792 6.544 3.256 6.603 4.842 1.278
## 
## [[8]]
## [1] 5.086 1.168 2.051 3.887 2.210 5.409 8.314 7.609
## 
## [[9]]
## [1] 6.3217 9.3295 0.8053 8.2122 7.5664 6.2449 7.9882 4.4039 6.1933
## 
## [[10]]
##  [1] 1.2048 4.7770 0.1617 4.6686 6.4722 2.3602 5.1982 0.5486 4.4962 4.0775

Example of annonymous
fuctions within lapply

object=list(a=matrix(1:4,2,2), b=matrix(1:6, 3,2))
object
## $a
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## $b
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
lapply(object, function(firstcolumn) firstcolumn[,1]) 
## $a
## [1] 1 2
## 
## $b
## [1] 1 2 3
lapply(object, function(firstrow) firstrow[1,]) 
## $a
## [1] 1 3
## 
## $b
## [1] 1 4

sapply

sapply is a user-friendly
version and wrapper of lapply by default returning a vector, matrix or,
if simplify = “array”, an array.

similar to lapply,
simplified, loop over a list, item by item

str(sapply)
## function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
object=list(a=1:10, b=rnorm(10))
object
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $b
##  [1]  0.5605  0.8882  0.3827  0.5794 -0.2564 -1.0797  1.1774  0.7096
##  [9] -0.3987 -0.9082
loutput=lapply(object, sd)
loutput
## $a
## [1] 3.028
## 
## $b
## [1] 0.7758
class(loutput)
## [1] "list"
soutput=sapply(object,sd) # notice difference
soutput
##      a      b 
## 3.0277 0.7758
class(soutput)
## [1] "numeric"

vapply

vapply is similar to
sapply, but has a pre-specified type of return value, so it can be
safer (and sometimes faster) to use.

apply

apply a function of your
choice over an array; not really looping

str(apply)
## function (X, MARGIN, FUN, ...)
#MARGIN  is a vector giving the subscripts which the function will be applied over;  for a matrix 1 indicates rows, 2 indicates columns
my_matrix<-matrix(rnorm(30), 10, 3)
apply(my_matrix, 2, mean) # apply mean on columns
## [1] -0.38317 -0.13071 -0.02697
apply(my_matrix, 1, mean) # apply mean on rows
##  [1]  9.349e-06  1.120e-01 -1.958e+00  1.879e-01  4.237e-01  6.855e-01
##  [7] -2.568e-01 -2.324e-01  4.607e-01 -1.226e+00
rowSums = apply(my_matrix, 1, sum)
rowSums
##  [1]  2.805e-05  3.360e-01 -5.873e+00  5.638e-01  1.271e+00  2.056e+00
##  [7] -7.704e-01 -6.973e-01  1.382e+00 -3.677e+00
rowMeans = apply(my_matrix, 1, mean)
rowMeans
##  [1]  9.349e-06  1.120e-01 -1.958e+00  1.879e-01  4.237e-01  6.855e-01
##  [7] -2.568e-01 -2.324e-01  4.607e-01 -1.226e+00
colSums = apply(my_matrix, 2, sum)
colSums
## [1] -3.8317 -1.3071 -0.2697
colMeans = apply(my_matrix, 2, mean)
colMeans
## [1] -0.38317 -0.13071 -0.02697
apply(my_matrix, 1, quantile, probs = c(0.25, 0.75))
##        [,1]   [,2]   [,3]    [,4]   [,5]   [,6]    [,7]    [,8]     [,9]
## 25% -0.2612 -0.417 -2.323 -0.1307 0.3783 0.1301 -0.6924 -0.9489 -0.02951
## 75%  0.4376  0.423 -1.651  0.4127 0.4870 1.1369  0.2504  0.4258  0.87513
##       [,10]
## 25% -1.7341
## 75% -0.9402
 a <- array(data=rnorm(2 * 2 * 10), dim=c(2, 2, 10)) #generate 40 random numbers and assign 2x2x10 dimentions
a
## , , 1
## 
##         [,1]   [,2]
## [1,] -0.4396 -0.184
## [2,]  1.0707  1.776
## 
## , , 2
## 
##         [,1]    [,2]
## [1,] -0.2107  0.1165
## [2,]  0.4991 -0.8378
## 
## , , 3
## 
##        [,1]    [,2]
## [1,] 1.6221  0.4075
## [2,] 0.1481 -0.4790
## 
## , , 4
## 
##         [,1]  [,2]
## [1,] -0.4467 1.560
## [2,] -0.3858 2.175
## 
## , , 5
## 
##         [,1]  [,2]
## [1,] -0.8019 1.474
## [2,] -0.3677 1.580
## 
## , , 6
## 
##         [,1]   [,2]
## [1,]  0.1808 -1.560
## [2,] -0.4412  2.058
## 
## , , 7
## 
##        [,1]     [,2]
## [1,] 0.8715 -0.01511
## [2,] 0.2059 -0.61433
## 
## , , 8
## 
##         [,1]    [,2]
## [1,]  0.6668 -0.4581
## [2,] -0.9788 -2.3214
## 
## , , 9
## 
##         [,1]    [,2]
## [1,] -0.4312  0.3950
## [2,] -0.5110 -0.8342
## 
## , , 10
## 
##        [,1]    [,2]
## [1,] 0.5953 -0.2219
## [2,] 0.2690 -0.4078

tapply

apply a function of your
choice over any subset of a vector Apply a function to each cell of a
ragged array, that is to each (non-empty) group of values given by a
unique combination of the levels of certain factors.

str(tapply)
## function (X, INDEX, FUN = NULL, ..., simplify = TRUE)
require(stats)
groups <- as.factor(rbinom(100, n = 5, prob = 0.8)) #rbinom(n, size, prob)
groups
## [1] 81 83 83 81 84
## Levels: 81 83 84
tapply(groups, groups, length) #- is almost the same as
## 81 83 84 
##  2  2  1
table(groups)
## groups
## 81 83 84 
##  2  2  1
x <- c(rnorm(10), runif(10), rnorm(10, 1))
x
##  [1]  0.14386 -0.21274 -0.31354 -0.32914 -0.74218 -0.41923  0.32376
##  [8]  0.57782  1.29516  0.51849  0.09856  0.74341  0.64724  0.40818
## [15]  0.80790  0.46450  0.92045  0.65810  0.96937  0.53147 -0.54966
## [22]  1.03216  1.40758  1.18356  3.07369  1.99872 -0.30349  2.04159
## [29]  0.87591  0.73995
f <- gl(3, 10) # generate 3 levels with 10 repetitions
#gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
#Generate factors by specifying the pattern of their levels.
f
##  [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
## Levels: 1 2 3
tapply(x, f, mean) #get the mean for each level in x
##       1       2       3 
## 0.08423 0.62492 1.15000
tapply(x, f, mean, simplify = FALSE) #group means without simplification
## $`1`
## [1] 0.08423
## 
## $`2`
## [1] 0.6249
## 
## $`3`
## [1] 1.15
tapply(x, f, range) # check group ranges
## $`1`
## [1] -0.7422  1.2952
## 
## $`2`
## [1] 0.09856 0.96937
## 
## $`3`
## [1] -0.5497  3.0737
tapply(x, f, min) # check group min
##        1        2        3 
## -0.74218  0.09856 -0.54966

split

Divide into Groups and
Reassemble. split divides the data in the vector x into the groups
defined by f.

The replacement forms
replace values corresponding to such a division.

unsplit reverses the
effect of split. Usage

split(x, f, drop = FALSE,
…) split(x, f, drop = FALSE, …) <- value unsplit(value, f, drop
= FALSE

x <- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3, 10)
x # no splitting in the data
##  [1] -0.64199  1.24759  0.94857 -1.91172  0.17663  0.10583  0.08753
##  [8]  0.44691  0.88841 -0.24475  0.61367  0.93486  0.74584  0.74547
## [15]  0.45167  0.36987  0.54612  0.66831  0.36644  0.44551  1.18085
## [22]  0.03665 -0.02822  2.46721  0.60715  1.50149  2.13073  1.17925
## [29] -0.27713 -0.75292
split(x,f) #splitted data by factors
## $`1`
##  [1] -0.64199  1.24759  0.94857 -1.91172  0.17663  0.10583  0.08753
##  [8]  0.44691  0.88841 -0.24475
## 
## $`2`
##  [1] 0.6137 0.9349 0.7458 0.7455 0.4517 0.3699 0.5461 0.6683 0.3664 0.4455
## 
## $`3`
##  [1]  1.18085  0.03665 -0.02822  2.46721  0.60715  1.50149  2.13073
##  [8]  1.17925 -0.27713 -0.75292
lapply(split(x, f), mean) # is same as:
## $`1`
## [1] 0.1103
## 
## $`2`
## [1] 0.5888
## 
## $`3`
## [1] 0.8045
sapply(split(x, f), mean) #is same as :
##      1      2      3 
## 0.1103 0.5888 0.8045
tapply(x, f, mean)
##      1      2      3 
## 0.1103 0.5888 0.8045

You can also split a data
frame

library(datasets)
head(airquality, 2)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
#Split air Quality data by month
s <- split(airquality, airquality$Month)
s
## $`5`
##    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
## 7     23     299  8.6   65     5   7
## 8     19      99 13.8   59     5   8
## 9      8      19 20.1   61     5   9
## 10    NA     194  8.6   69     5  10
## 11     7      NA  6.9   74     5  11
## 12    16     256  9.7   69     5  12
## 13    11     290  9.2   66     5  13
## 14    14     274 10.9   68     5  14
## 15    18      65 13.2   58     5  15
## 16    14     334 11.5   64     5  16
## 17    34     307 12.0   66     5  17
## 18     6      78 18.4   57     5  18
## 19    30     322 11.5   68     5  19
## 20    11      44  9.7   62     5  20
## 21     1       8  9.7   59     5  21
## 22    11     320 16.6   73     5  22
## 23     4      25  9.7   61     5  23
## 24    32      92 12.0   61     5  24
## 25    NA      66 16.6   57     5  25
## 26    NA     266 14.9   58     5  26
## 27    NA      NA  8.0   57     5  27
## 28    23      13 12.0   67     5  28
## 29    45     252 14.9   81     5  29
## 30   115     223  5.7   79     5  30
## 31    37     279  7.4   76     5  31
## 
## $`6`
##    Ozone Solar.R Wind Temp Month Day
## 32    NA     286  8.6   78     6   1
## 33    NA     287  9.7   74     6   2
## 34    NA     242 16.1   67     6   3
## 35    NA     186  9.2   84     6   4
## 36    NA     220  8.6   85     6   5
## 37    NA     264 14.3   79     6   6
## 38    29     127  9.7   82     6   7
## 39    NA     273  6.9   87     6   8
## 40    71     291 13.8   90     6   9
## 41    39     323 11.5   87     6  10
## 42    NA     259 10.9   93     6  11
## 43    NA     250  9.2   92     6  12
## 44    23     148  8.0   82     6  13
## 45    NA     332 13.8   80     6  14
## 46    NA     322 11.5   79     6  15
## 47    21     191 14.9   77     6  16
## 48    37     284 20.7   72     6  17
## 49    20      37  9.2   65     6  18
## 50    12     120 11.5   73     6  19
## 51    13     137 10.3   76     6  20
## 52    NA     150  6.3   77     6  21
## 53    NA      59  1.7   76     6  22
## 54    NA      91  4.6   76     6  23
## 55    NA     250  6.3   76     6  24
## 56    NA     135  8.0   75     6  25
## 57    NA     127  8.0   78     6  26
## 58    NA      47 10.3   73     6  27
## 59    NA      98 11.5   80     6  28
## 60    NA      31 14.9   77     6  29
## 61    NA     138  8.0   83     6  30
## 
## $`7`
##    Ozone Solar.R Wind Temp Month Day
## 62   135     269  4.1   84     7   1
## 63    49     248  9.2   85     7   2
## 64    32     236  9.2   81     7   3
## 65    NA     101 10.9   84     7   4
## 66    64     175  4.6   83     7   5
## 67    40     314 10.9   83     7   6
## 68    77     276  5.1   88     7   7
## 69    97     267  6.3   92     7   8
## 70    97     272  5.7   92     7   9
## 71    85     175  7.4   89     7  10
## 72    NA     139  8.6   82     7  11
## 73    10     264 14.3   73     7  12
## 74    27     175 14.9   81     7  13
## 75    NA     291 14.9   91     7  14
## 76     7      48 14.3   80     7  15
## 77    48     260  6.9   81     7  16
## 78    35     274 10.3   82     7  17
## 79    61     285  6.3   84     7  18
## 80    79     187  5.1   87     7  19
## 81    63     220 11.5   85     7  20
## 82    16       7  6.9   74     7  21
## 83    NA     258  9.7   81     7  22
## 84    NA     295 11.5   82     7  23
## 85    80     294  8.6   86     7  24
## 86   108     223  8.0   85     7  25
## 87    20      81  8.6   82     7  26
## 88    52      82 12.0   86     7  27
## 89    82     213  7.4   88     7  28
## 90    50     275  7.4   86     7  29
## 91    64     253  7.4   83     7  30
## 92    59     254  9.2   81     7  31
## 
## $`8`
##     Ozone Solar.R Wind Temp Month Day
## 93     39      83  6.9   81     8   1
## 94      9      24 13.8   81     8   2
## 95     16      77  7.4   82     8   3
## 96     78      NA  6.9   86     8   4
## 97     35      NA  7.4   85     8   5
## 98     66      NA  4.6   87     8   6
## 99    122     255  4.0   89     8   7
## 100    89     229 10.3   90     8   8
## 101   110     207  8.0   90     8   9
## 102    NA     222  8.6   92     8  10
## 103    NA     137 11.5   86     8  11
## 104    44     192 11.5   86     8  12
## 105    28     273 11.5   82     8  13
## 106    65     157  9.7   80     8  14
## 107    NA      64 11.5   79     8  15
## 108    22      71 10.3   77     8  16
## 109    59      51  6.3   79     8  17
## 110    23     115  7.4   76     8  18
## 111    31     244 10.9   78     8  19
## 112    44     190 10.3   78     8  20
## 113    21     259 15.5   77     8  21
## 114     9      36 14.3   72     8  22
## 115    NA     255 12.6   75     8  23
## 116    45     212  9.7   79     8  24
## 117   168     238  3.4   81     8  25
## 118    73     215  8.0   86     8  26
## 119    NA     153  5.7   88     8  27
## 120    76     203  9.7   97     8  28
## 121   118     225  2.3   94     8  29
## 122    84     237  6.3   96     8  30
## 123    85     188  6.3   94     8  31
## 
## $`9`
##     Ozone Solar.R Wind Temp Month Day
## 124    96     167  6.9   91     9   1
## 125    78     197  5.1   92     9   2
## 126    73     183  2.8   93     9   3
## 127    91     189  4.6   93     9   4
## 128    47      95  7.4   87     9   5
## 129    32      92 15.5   84     9   6
## 130    20     252 10.9   80     9   7
## 131    23     220 10.3   78     9   8
## 132    21     230 10.9   75     9   9
## 133    24     259  9.7   73     9  10
## 134    44     236 14.9   81     9  11
## 135    21     259 15.5   76     9  12
## 136    28     238  6.3   77     9  13
## 137     9      24 10.9   71     9  14
## 138    13     112 11.5   71     9  15
## 139    46     237  6.9   78     9  16
## 140    18     224 13.8   67     9  17
## 141    13      27 10.3   76     9  18
## 142    24     238 10.3   68     9  19
## 143    16     201  8.0   82     9  20
## 144    13     238 12.6   64     9  21
## 145    23      14  9.2   71     9  22
## 146    36     139 10.3   81     9  23
## 147     7      49 10.3   69     9  24
## 148    14      20 16.6   63     9  25
## 149    30     193  6.9   70     9  26
## 150    NA     145 13.2   77     9  27
## 151    14     191 14.3   75     9  28
## 152    18     131  8.0   76     9  29
## 153    20     223 11.5   68     9  30
lapply(s, function(x) colMeans(x[, c("Ozone", "Solar.R", "Wind")]))
## $`5`
##   Ozone Solar.R    Wind 
##      NA      NA   11.62 
## 
## $`6`
##   Ozone Solar.R    Wind 
##      NA  190.17   10.27 
## 
## $`7`
##   Ozone Solar.R    Wind 
##      NA 216.484   8.942 
## 
## $`8`
##   Ozone Solar.R    Wind 
##      NA      NA   8.794 
## 
## $`9`
##   Ozone Solar.R    Wind 
##      NA  167.43   10.18
colMeans(airquality) # for all columns
##   Ozone Solar.R    Wind    Temp   Month     Day 
##      NA      NA   9.958  77.882   6.993  15.804
sapply(s, function(x) colMeans(x[, c("Ozone", "Solar.R", "Wind")]))
##             5      6       7     8      9
## Ozone      NA     NA      NA    NA     NA
## Solar.R    NA 190.17 216.484    NA 167.43
## Wind    11.62  10.27   8.942 8.794  10.18
#Now try removing NAs from the dataset
sapply(s, function(x) colMeans(x[, c("Ozone", "Solar.R", "Wind")], na.rm = TRUE))
##              5      6       7       8      9
## Ozone    23.62  29.44  59.115  59.962  31.45
## Solar.R 181.30 190.17 216.484 171.857 167.43
## Wind     11.62  10.27   8.942   8.794  10.18

You can also split more
than one level with SPLIT command

x=rnorm(20)
x
##  [1]  2.48597  1.34398  0.57253  1.69531  0.75884  0.17921  0.14734
##  [8]  1.09718  0.07409 -2.00592 -1.49990 -2.23533  0.31148  0.68989
## [15]  2.61388 -1.32126  1.75524  0.36550 -0.76513  0.24786
factor1=gl(4,5)
factor1
##  [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
## Levels: 1 2 3 4
factor2=gl(5,4)
factor2
##  [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
## Levels: 1 2 3 4 5
interaction(factor1, factor2)
##  [1] 1.1 1.1 1.1 1.1 1.2 2.2 2.2 2.2 2.3 2.3 3.3 3.3 3.4 3.4 3.4 4.4 4.5
## [18] 4.5 4.5 4.5
## 20 Levels: 1.1 2.1 3.1 4.1 1.2 2.2 3.2 4.2 1.3 2.3 3.3 4.3 1.4 2.4 ... 4.5
str(split(x, list(factor1, factor2))) # this creates empty levels which can be dropped using
## List of 20
##  $ 1.1: num [1:4] 2.486 1.344 0.573 1.695
##  $ 2.1: num(0) 
##  $ 3.1: num(0) 
##  $ 4.1: num(0) 
##  $ 1.2: num 0.759
##  $ 2.2: num [1:3] 0.179 0.147 1.097
##  $ 3.2: num(0) 
##  $ 4.2: num(0) 
##  $ 1.3: num(0) 
##  $ 2.3: num [1:2] 0.0741 -2.0059
##  $ 3.3: num [1:2] -1.5 -2.24
##  $ 4.3: num(0) 
##  $ 1.4: num(0) 
##  $ 2.4: num(0) 
##  $ 3.4: num [1:3] 0.311 0.69 2.614
##  $ 4.4: num -1.32
##  $ 1.5: num(0) 
##  $ 2.5: num(0) 
##  $ 3.5: num(0) 
##  $ 4.5: num [1:4] 1.755 0.365 -0.765 0.248
str(split(x, list(factor1, factor2), drop = TRUE))
## List of 8
##  $ 1.1: num [1:4] 2.486 1.344 0.573 1.695
##  $ 1.2: num 0.759
##  $ 2.2: num [1:3] 0.179 0.147 1.097
##  $ 2.3: num [1:2] 0.0741 -2.0059
##  $ 3.3: num [1:2] -1.5 -2.24
##  $ 3.4: num [1:3] 0.311 0.69 2.614
##  $ 4.4: num -1.32
##  $ 4.5: num [1:4] 1.755 0.365 -0.765 0.248

mapply

this is the multivariate
version of tapply. Apply a Function to Multiple List or Vector
Arguments.

mapply is a multivariate
version of sapply. mapply applies FUN to the first elements of each …
argument, the second elements, the third elements, and so on. Arguments
are recycled if necessary.

mapply(rep, 1:4, 4:1)
## [[1]]
## [1] 1 1 1 1
## 
## [[2]]
## [1] 2 2 2
## 
## [[3]]
## [1] 3 3
## 
## [[4]]
## [1] 4
mapply(rep, times = 1:4, x = 4:1)
## [[1]]
## [1] 4
## 
## [[2]]
## [1] 3 3
## 
## [[3]]
## [1] 2 2 2
## 
## [[4]]
## [1] 1 1 1 1

More
Examples

require(stats); require(graphics)

x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
# compute the list mean for each list element
lapply(x, mean)
## $a
## [1] 5.5
## 
## $beta
## [1] 4.535
## 
## $logic
## [1] 0.5
# median and quartiles for each list element
x
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $beta
## [1]  0.04979  0.13534  0.36788  1.00000  2.71828  7.38906 20.08554
## 
## $logic
## [1]  TRUE FALSE FALSE  TRUE
lapply(x, quantile, probs = 1:3/4)
## $a
##  25%  50%  75% 
## 3.25 5.50 7.75 
## 
## $beta
##    25%    50%    75% 
## 0.2516 1.0000 5.0537 
## 
## $logic
## 25% 50% 75% 
## 0.0 0.5 1.0
x
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $beta
## [1]  0.04979  0.13534  0.36788  1.00000  2.71828  7.38906 20.08554
## 
## $logic
## [1]  TRUE FALSE FALSE  TRUE
sapply(x, quantile)
##          a     beta logic
## 0%    1.00  0.04979   0.0
## 25%   3.25  0.25161   0.0
## 50%   5.50  1.00000   0.5
## 75%   7.75  5.05367   1.0
## 100% 10.00 20.08554   1.0
i39 <- sapply(3:9, seq) # list of vectors
i39
## [[1]]
## [1] 1 2 3
## 
## [[2]]
## [1] 1 2 3 4
## 
## [[3]]
## [1] 1 2 3 4 5
## 
## [[4]]
## [1] 1 2 3 4 5 6
## 
## [[5]]
## [1] 1 2 3 4 5 6 7
## 
## [[6]]
## [1] 1 2 3 4 5 6 7 8
## 
## [[7]]
## [1] 1 2 3 4 5 6 7 8 9
sapply(i39, fivenum)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]  1.0  1.0    1  1.0  1.0  1.0    1
## [2,]  1.5  1.5    2  2.0  2.5  2.5    3
## [3,]  2.0  2.5    3  3.5  4.0  4.5    5
## [4,]  2.5  3.5    4  5.0  5.5  6.5    7
## [5,]  3.0  4.0    5  6.0  7.0  8.0    9

 

Leave a Comment

Your email address will not be published. Required fields are marked *