๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/R

apply, sapply, lapply ์‚ฌ์šฉ๋ฒ•

apply, sapply, lapply ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•˜์—ฌ ๋ณด์ž.

 

In:

library(dplyr)

df_iris = iris

df_iris_num = iris %>% 
  select(-Species)
  
str(df_iris_num)

 

Out:

'data.frame':	150 obs. of  4 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...

 

โ–ท Species ์—ด์„ ์ œ์™ธํ•œ Iris ๋ฐ์ดํ„ฐ๋ฅผ ์‹คํ—˜ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

โ–ก apply ํ•จ์ˆ˜

 

In:

apply(X = df_iris_num, MARGIN = 1, FUN = mean)
apply(X = df_iris_num, MARGIN = 2, FUN = mean)

 

Out:

  [1] 2.550 2.375 2.350 2.350 2.550 2.850 2.425 2.525 2.225 2.400 2.700 2.500 2.325 2.125 2.800 3.000 2.750 2.575 2.875 2.675 2.675 2.675 2.350
 [24] 2.650 2.575 2.450 2.600 2.600 2.550 2.425 2.425 2.675 2.725 2.825 2.425 2.400 2.625 2.500 2.225 2.550 2.525 2.100 2.275 2.675 2.800 2.375
 [47] 2.675 2.350 2.675 2.475 4.075 3.900 4.100 3.275 3.850 3.575 3.975 2.900 3.850 3.300 2.875 3.650 3.300 3.775 3.350 3.900 3.650 3.400 3.600
 [70] 3.275 3.925 3.550 3.800 3.700 3.725 3.850 3.950 4.100 3.725 3.200 3.200 3.150 3.400 3.850 3.600 3.875 4.000 3.575 3.500 3.325 3.425 3.775
 [93] 3.400 2.900 3.450 3.525 3.525 3.675 2.925 3.475 4.525 3.875 4.525 4.150 4.375 4.825 3.400 4.575 4.200 4.850 4.200 4.075 4.350 3.800 4.025
[116] 4.300 4.200 5.100 4.875 3.675 4.525 3.825 4.800 3.925 4.450 4.550 3.900 3.950 4.225 4.400 4.550 5.025 4.250 3.925 3.925 4.775 4.425 4.200
[139] 3.900 4.375 4.450 4.350 3.875 4.550 4.550 4.300 3.925 4.175 4.325 3.950

Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 

 

โ–ท ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ FUN ์ธ์ž์— ํ• ๋‹น๋œ mean ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. apply ํ•จ์ˆ˜์˜ MARGIN ์ธ์ž๋Š” ์—ฐ์‚ฐ์˜ ๋ฐฉํ–ฅ์„ ๊ฒฐ์ •ํ•œ๋‹ค. 1์ธ ๊ฒฝ์šฐ, ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ, 2์ธ ๊ฒฝ์šฐ, ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. 

 

In:

apply(X = df_iris_num, MARGIN = 1, FUN = function(x) {x*2+1})
apply(X = df_iris_num, MARGIN = 2, FUN = function(x) {x*2+1})

 

Out:

             [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
Sepal.Length 11.2 10.8 10.4 10.2 11.0 11.8 10.2 11.0  9.8  10.8  11.8  10.6  10.6   9.6  12.6  12.4  11.8  11.2  12.4  11.2  11.8  11.2  10.2
Sepal.Width   8.0  7.0  7.4  7.2  8.2  8.8  7.8  7.8  6.8   7.2   8.4   7.8   7.0   7.0   9.0   9.8   8.8   8.0   8.6   8.6   7.8   8.4   8.2
Petal.Length  3.8  3.8  3.6  4.0  3.8  4.4  3.8  4.0  3.8   4.0   4.0   4.2   3.8   3.2   3.4   4.0   3.6   3.8   4.4   4.0   4.4   4.0   3.0
Petal.Width   1.4  1.4  1.4  1.4  1.4  1.8  1.6  1.4  1.4   1.2   1.4   1.4   1.2   1.2   1.4   1.8   1.8   1.6   1.6   1.6   1.4   1.8   1.4
             [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
Sepal.Length  11.2  10.6  11.0  11.0  11.4  11.4  10.4  10.6  11.8  11.4  12.0  10.8  11.0  12.0  10.8   9.8  11.2  11.0  10.0   9.8  11.0  11.2
Sepal.Width    7.6   7.8   7.0   7.8   8.0   7.8   7.4   7.2   7.8   9.2   9.4   7.2   7.4   8.0   8.2   7.0   7.8   8.0   5.6   7.4   8.0   8.6
Petal.Length   4.4   4.8   4.2   4.2   4.0   3.8   4.2   4.2   4.0   4.0   3.8   4.0   3.4   3.6   3.8   3.6   4.0   3.6   3.6   3.6   4.2   4.8
Petal.Width    2.0   1.4   1.4   1.8   1.4   1.4   1.4   1.4   1.8   1.2   1.4   1.4   1.4   1.4   1.2   1.4   1.4   1.6   1.6   1.4   2.2   1.8
             [,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65] [,66] [,67]
Sepal.Length  10.6  11.2  10.2  11.6  11.0  15.0  13.8  14.8  12.0  14.0  12.4  13.6  10.8  14.2  11.4    11  12.8  13.0  13.2  12.2  14.4  12.2
Sepal.Width    7.0   8.6   7.4   8.4   7.6   7.4   7.4   7.2   5.6   6.6   6.6   7.6   5.8   6.8   6.4     5   7.0   5.4   6.8   6.8   7.2   7.0
Petal.Length   3.8   4.2   3.8   4.0   3.8  10.4  10.0  10.8   9.0  10.2  10.0  10.4   7.6  10.2   8.8     8   9.4   9.0  10.4   8.2   9.8  10.0
Petal.Width    1.6   1.4   1.4   1.4   1.4   3.8   4.0   4.0   3.6   4.0   3.6   4.2   3.0   3.6   3.8     3   4.0   3.0   3.8   3.6   3.8   4.0
             [,68] [,69] [,70] [,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89]
Sepal.Length  12.6  13.4  12.2  12.8  13.2  13.6  13.2  13.8  14.2  14.6  14.4  13.0  12.4  12.0  12.0  12.6  13.0  11.8  13.0  14.4  13.6  12.2
Sepal.Width    6.4   5.4   6.0   7.4   6.6   6.0   6.6   6.8   7.0   6.6   7.0   6.8   6.2   5.8   5.8   6.4   6.4   7.0   7.8   7.2   5.6   7.0
Petal.Length   9.2  10.0   8.8  10.6   9.0  10.8  10.4   9.6   9.8  10.6  11.0  10.0   8.0   8.6   8.4   8.8  11.2  10.0  10.0  10.4   9.8   9.2
Petal.Width    3.0   4.0   3.2   4.6   3.6   4.0   3.4   3.6   3.8   3.8   4.4   4.0   3.0   3.2   3.0   3.4   4.2   4.0   4.2   4.0   3.6   3.6
             [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100] [,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109]
Sepal.Length  12.0  12.0  13.2  12.6  11.0  12.2  12.4  12.4  13.4  11.2   12.4   13.6   12.6   15.2   13.6   14.0   16.2   10.8   15.6   14.4
Sepal.Width    6.0   6.2   7.0   6.2   5.6   6.4   7.0   6.8   6.8   6.0    6.6    7.6    6.4    7.0    6.8    7.0    7.0    6.0    6.8    6.0
Petal.Length   9.0   9.8  10.2   9.0   7.6   9.4   9.4   9.4   9.6   7.0    9.2   13.0   11.2   12.8   12.2   12.6   14.2   10.0   13.6   12.6
Petal.Width    3.6   3.4   3.8   3.4   3.0   3.6   3.4   3.6   3.6   3.2    3.6    6.0    4.8    5.2    4.6    5.4    5.2    4.4    4.6    4.6
             [,110] [,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120] [,121] [,122] [,123] [,124] [,125] [,126] [,127]
Sepal.Length   15.4   14.0   13.8   14.6   12.4   12.6   13.8   14.0   16.4   16.4   13.0   14.8   12.2   16.4   13.6   14.4   15.4   13.4
Sepal.Width     8.2    7.4    6.4    7.0    6.0    6.6    7.4    7.0    8.6    6.2    5.4    7.4    6.6    6.6    6.4    7.6    7.4    6.6
Petal.Length   13.2   11.2   11.6   12.0   11.0   11.2   11.6   12.0   14.4   14.8   11.0   12.4   10.8   14.4   10.8   12.4   13.0   10.6
Petal.Width     6.0    5.0    4.8    5.2    5.0    5.8    5.6    4.6    5.4    5.6    4.0    5.6    5.0    5.0    4.6    5.2    4.6    4.6
             [,128] [,129] [,130] [,131] [,132] [,133] [,134] [,135] [,136] [,137] [,138] [,139] [,140] [,141] [,142] [,143] [,144] [,145]
Sepal.Length   13.2   13.8   15.4   15.8   16.8   13.8   13.6   13.2   16.4   13.6   13.8   13.0   14.8   14.4   14.8   12.6   14.6   14.4
Sepal.Width     7.0    6.6    7.0    6.6    8.6    6.6    6.6    6.2    7.0    7.8    7.2    7.0    7.2    7.2    7.2    6.4    7.4    7.6
Petal.Length   10.8   12.2   12.6   13.2   13.8   12.2   11.2   12.2   13.2   12.2   12.0   10.6   11.8   12.2   11.2   11.2   12.8   12.4
Petal.Width     4.6    5.2    4.2    4.8    5.0    5.4    4.0    3.8    5.6    5.8    4.6    4.6    5.2    5.8    5.6    4.8    5.6    6.0
             [,146] [,147] [,148] [,149] [,150]
Sepal.Length   14.4   13.6   14.0   13.4   12.8
Sepal.Width     7.0    6.0    7.0    7.8    7.0
Petal.Length   11.4   11.0   11.4   11.8   11.2
Petal.Width     5.6    4.8    5.0    5.6    4.6

       Sepal.Length Sepal.Width Petal.Length Petal.Width
  [1,]         11.2         8.0          3.8         1.4
  [2,]         10.8         7.0          3.8         1.4
  [3,]         10.4         7.4          3.6         1.4
  [4,]         10.2         7.2          4.0         1.4
  [5,]         11.0         8.2          3.8         1.4
  [6,]         11.8         8.8          4.4         1.8
  [7,]         10.2         7.8          3.8         1.6
  [8,]         11.0         7.8          4.0         1.4
  [9,]          9.8         6.8          3.8         1.4
 [10,]         10.8         7.2          4.0         1.2
 [11,]         11.8         8.4          4.0         1.4
 [12,]         10.6         7.8          4.2         1.4
 [13,]         10.6         7.0          3.8         1.2
 [14,]          9.6         7.0          3.2         1.2
 [15,]         12.6         9.0          3.4         1.4
 [16,]         12.4         9.8          4.0         1.8
 [17,]         11.8         8.8          3.6         1.8
 [18,]         11.2         8.0          3.8         1.6
 [19,]         12.4         8.6          4.4         1.6
 [20,]         11.2         8.6          4.0         1.6
 [21,]         11.8         7.8          4.4         1.4
 [22,]         11.2         8.4          4.0         1.8
 [23,]         10.2         8.2          3.0         1.4
 [24,]         11.2         7.6          4.4         2.0
 [25,]         10.6         7.8          4.8         1.4
 [26,]         11.0         7.0          4.2         1.4
 [27,]         11.0         7.8          4.2         1.8
 [28,]         11.4         8.0          4.0         1.4
 [29,]         11.4         7.8          3.8         1.4
 [30,]         10.4         7.4          4.2         1.4
 [31,]         10.6         7.2          4.2         1.4
 [32,]         11.8         7.8          4.0         1.8
 [33,]         11.4         9.2          4.0         1.2
 [34,]         12.0         9.4          3.8         1.4
 [35,]         10.8         7.2          4.0         1.4
 [36,]         11.0         7.4          3.4         1.4
 [37,]         12.0         8.0          3.6         1.4
 [38,]         10.8         8.2          3.8         1.2
 [39,]          9.8         7.0          3.6         1.4
 [40,]         11.2         7.8          4.0         1.4
 [41,]         11.0         8.0          3.6         1.6
 [42,]         10.0         5.6          3.6         1.6
 [43,]          9.8         7.4          3.6         1.4
 [44,]         11.0         8.0          4.2         2.2
 [45,]         11.2         8.6          4.8         1.8
 [46,]         10.6         7.0          3.8         1.6
 [47,]         11.2         8.6          4.2         1.4
 [48,]         10.2         7.4          3.8         1.4
 [49,]         11.6         8.4          4.0         1.4
 [50,]         11.0         7.6          3.8         1.4
 [51,]         15.0         7.4         10.4         3.8
 [52,]         13.8         7.4         10.0         4.0
 [53,]         14.8         7.2         10.8         4.0
 [54,]         12.0         5.6          9.0         3.6
 [55,]         14.0         6.6         10.2         4.0
 [56,]         12.4         6.6         10.0         3.6
 [57,]         13.6         7.6         10.4         4.2
 [58,]         10.8         5.8          7.6         3.0
 [59,]         14.2         6.8         10.2         3.6
 [60,]         11.4         6.4          8.8         3.8
 [61,]         11.0         5.0          8.0         3.0
 [62,]         12.8         7.0          9.4         4.0
 [63,]         13.0         5.4          9.0         3.0
 [64,]         13.2         6.8         10.4         3.8
 [65,]         12.2         6.8          8.2         3.6
 [66,]         14.4         7.2          9.8         3.8
 [67,]         12.2         7.0         10.0         4.0
 [68,]         12.6         6.4          9.2         3.0
 [69,]         13.4         5.4         10.0         4.0
 [70,]         12.2         6.0          8.8         3.2
 [71,]         12.8         7.4         10.6         4.6
 [72,]         13.2         6.6          9.0         3.6
 [73,]         13.6         6.0         10.8         4.0
 [74,]         13.2         6.6         10.4         3.4
 [75,]         13.8         6.8          9.6         3.6
 [76,]         14.2         7.0          9.8         3.8
 [77,]         14.6         6.6         10.6         3.8
 [78,]         14.4         7.0         11.0         4.4
 [79,]         13.0         6.8         10.0         4.0
 [80,]         12.4         6.2          8.0         3.0
 [81,]         12.0         5.8          8.6         3.2
 [82,]         12.0         5.8          8.4         3.0
 [83,]         12.6         6.4          8.8         3.4
 [84,]         13.0         6.4         11.2         4.2
 [85,]         11.8         7.0         10.0         4.0
 [86,]         13.0         7.8         10.0         4.2
 [87,]         14.4         7.2         10.4         4.0
 [88,]         13.6         5.6          9.8         3.6
 [89,]         12.2         7.0          9.2         3.6
 [90,]         12.0         6.0          9.0         3.6
 [91,]         12.0         6.2          9.8         3.4
 [92,]         13.2         7.0         10.2         3.8
 [93,]         12.6         6.2          9.0         3.4
 [94,]         11.0         5.6          7.6         3.0
 [95,]         12.2         6.4          9.4         3.6
 [96,]         12.4         7.0          9.4         3.4
 [97,]         12.4         6.8          9.4         3.6
 [98,]         13.4         6.8          9.6         3.6
 [99,]         11.2         6.0          7.0         3.2
[100,]         12.4         6.6          9.2         3.6
[101,]         13.6         7.6         13.0         6.0
[102,]         12.6         6.4         11.2         4.8
[103,]         15.2         7.0         12.8         5.2
[104,]         13.6         6.8         12.2         4.6
[105,]         14.0         7.0         12.6         5.4
[106,]         16.2         7.0         14.2         5.2
[107,]         10.8         6.0         10.0         4.4
[108,]         15.6         6.8         13.6         4.6
[109,]         14.4         6.0         12.6         4.6
[110,]         15.4         8.2         13.2         6.0
[111,]         14.0         7.4         11.2         5.0
[112,]         13.8         6.4         11.6         4.8
[113,]         14.6         7.0         12.0         5.2
[114,]         12.4         6.0         11.0         5.0
[115,]         12.6         6.6         11.2         5.8
[116,]         13.8         7.4         11.6         5.6
[117,]         14.0         7.0         12.0         4.6
[118,]         16.4         8.6         14.4         5.4
[119,]         16.4         6.2         14.8         5.6
[120,]         13.0         5.4         11.0         4.0
[121,]         14.8         7.4         12.4         5.6
[122,]         12.2         6.6         10.8         5.0
[123,]         16.4         6.6         14.4         5.0
[124,]         13.6         6.4         10.8         4.6
[125,]         14.4         7.6         12.4         5.2
[126,]         15.4         7.4         13.0         4.6
[127,]         13.4         6.6         10.6         4.6
[128,]         13.2         7.0         10.8         4.6
[129,]         13.8         6.6         12.2         5.2
[130,]         15.4         7.0         12.6         4.2
[131,]         15.8         6.6         13.2         4.8
[132,]         16.8         8.6         13.8         5.0
[133,]         13.8         6.6         12.2         5.4
[134,]         13.6         6.6         11.2         4.0
[135,]         13.2         6.2         12.2         3.8
[136,]         16.4         7.0         13.2         5.6
[137,]         13.6         7.8         12.2         5.8
[138,]         13.8         7.2         12.0         4.6
[139,]         13.0         7.0         10.6         4.6
[140,]         14.8         7.2         11.8         5.2
[141,]         14.4         7.2         12.2         5.8
[142,]         14.8         7.2         11.2         5.6
[143,]         12.6         6.4         11.2         4.8
[144,]         14.6         7.4         12.8         5.6
[145,]         14.4         7.6         12.4         6.0
[146,]         14.4         7.0         11.4         5.6
[147,]         13.6         6.0         11.0         4.8
[148,]         14.0         7.0         11.4         5.0
[149,]         13.4         7.8         11.8         5.6
[150,]         12.8         7.0         11.2         4.6

 

โ–ท FUN ํ•จ์ˆ˜์— function ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์—ฐ์‚ฐ์„ ๊ตฌ์ฒดํ™”ํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์—ฐ์‚ฐ์—์„œ๋Š” ๊ฐ ์›์†Œ์— ๋Œ€ํ•˜์—ฌ 2๋ฅผ ๊ณฑํ•œ ํ›„, 1์„ ๋”ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ํ–‰๊ณผ ์—ด์˜ ์ˆœ์„œ๋งŒ ๋‹ค๋ฅผ ๋ฟ, ์›์†Œ๋Š” ๊ฐ™์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

In:

apply(X = df_iris_num, MARGIN = 1, FUN = function(x) {median(x*2+1)})
apply(X = df_iris_num, MARGIN = 2, FUN = function(x) {median(x*2+1)})

 

Out:

  [1]  5.9  5.4  5.5  5.6  6.0  6.6  5.8  5.9  5.3  5.6  6.2  6.0  5.4  5.1  6.2  6.9  6.2  5.9  6.5  6.3  6.1  6.2  5.6  6.0  6.3  5.6  6.0  6.0
 [29]  5.8  5.8  5.7  5.9  6.6  6.6  5.6  5.4  5.8  6.0  5.3  5.9  5.8  4.6  5.5  6.1  6.7  5.4  6.4  5.6  6.2  5.7  8.9  8.7  9.0  7.3  8.4  8.3
 [57]  9.0  6.7  8.5  7.6  6.5  8.2  7.2  8.6  7.5  8.5  8.5  7.8  7.7  7.4  9.0  7.8  8.4  8.5  8.2  8.4  8.6  9.0  8.4  7.1  7.2  7.1  7.6  8.8
 [85]  8.5  8.9  8.8  7.7  8.1  7.5  8.0  8.6  7.6  6.6  7.9  8.2  8.1  8.2  6.5  7.9 10.3  8.8  9.9  9.5  9.8 10.6  8.0 10.2  9.3 10.7  9.3  9.0
[113]  9.5  8.5  8.9  9.5  9.5 11.5 10.5  8.2  9.9  8.7 10.5  8.6 10.0 10.2  8.6  8.9  9.4  9.8  9.9 11.2  9.4  8.9  9.2 10.1 10.0  9.6  8.8  9.5
[141]  9.7  9.2  8.8 10.1 10.0  9.2  8.5  9.2  9.8  9.1

Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
        12.6          7.0          9.7          3.6 

 

โ–ท function ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์›์†Œ์— 2๋ฅผ ๊ณฑํ•˜๊ณ , 1์„ ๋”ํ•œ ํ›„์˜ ์ค‘๊ฐ„๊ฐ’์„ ๊ตฌํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

 

โ–ถ apply ํ•จ์ˆ˜๋Š” ๋ฒกํ„ฐ ๊ธฐ๋ฐ˜์˜ ๋ณ‘๋ ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, for ๋˜๋Š” while์„ ์ด์šฉํ•œ ์—ฐ์‚ฐ๋ณด๋‹ค ๋น ๋ฅด๋‹ค. ๋˜ํ•œ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

โ–ก sapply & lapply ํ•จ์ˆ˜

 

In:

sapply(X = df_iris_num, FUN = mean)

sapply(X = df_iris_num, FUN = mean, simplify = F)

lapply(X = df_iris_num, FUN = mean)

 

Out:

Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 


$Sepal.Length
[1] 5.843333

$Sepal.Width
[1] 3.057333

$Petal.Length
[1] 3.758

$Petal.Width
[1] 1.199333


$Sepal.Length
[1] 5.843333

$Sepal.Width
[1] 3.057333

$Petal.Length
[1] 3.758

$Petal.Width
[1] 1.199333

 

โ–ท sapply์™€ lapply๋Š” ์•ž์˜ apply ํ•จ์ˆ˜์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ฒกํ„ฐ ๋˜๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” sapply๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒกํ„ฐ์˜ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” simplify ์ธ์ž๋ฅผ F๋กœ ์ฃผ์–ด, ์„ธ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. lapply๋Š” ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , sapply ํ•จ์ˆ˜์˜ simplify ์ธ์ž์— F๋ฅผ ์ค„ ๊ฒฝ์šฐ, lapply์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ๋œ๋‹ค.