library(nycflights13)
library(tidyverse) 
library(tibble)
library(purrr)

data(mtcars)
data(flights)
data(iris)
str(mtcars);str(flights);str(iris)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
## Classes 'tbl_df', 'tbl' and 'data.frame':    336776 obs. of  19 variables:
##  $ year          : int  2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 ...
##  $ month         : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ day           : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ dep_time      : int  517 533 542 544 554 554 555 557 557 558 ...
##  $ sched_dep_time: int  515 529 540 545 600 558 600 600 600 600 ...
##  $ dep_delay     : num  2 4 2 -1 -6 -4 -5 -3 -3 -2 ...
##  $ arr_time      : int  830 850 923 1004 812 740 913 709 838 753 ...
##  $ sched_arr_time: int  819 830 850 1022 837 728 854 723 846 745 ...
##  $ arr_delay     : num  11 20 33 -18 -25 12 19 -14 -8 8 ...
##  $ carrier       : chr  "UA" "UA" "AA" "B6" ...
##  $ flight        : int  1545 1714 1141 725 461 1696 507 5708 79 301 ...
##  $ tailnum       : chr  "N14228" "N24211" "N619AA" "N804JB" ...
##  $ origin        : chr  "EWR" "LGA" "JFK" "JFK" ...
##  $ dest          : chr  "IAH" "IAH" "MIA" "BQN" ...
##  $ air_time      : num  227 227 160 183 116 150 158 53 140 138 ...
##  $ distance      : num  1400 1416 1089 1576 762 ...
##  $ hour          : num  5 5 5 5 6 5 6 6 6 6 ...
##  $ minute        : num  15 29 40 45 0 58 0 0 0 0 ...
##  $ time_hour     : POSIXct, format: "2013-01-01 05:00:00" "2013-01-01 05:00:00" ...
## 'data.frame':    150 obs. of  5 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     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
output <- c("double", ncol(mtcars))
for (i in seq_along(mtcars)) {
  output[[i]] <- mean(mtcars[[i]])
}
output
##  [1] "20.090625"  "6.1875"     "230.721875" "146.6875"   "3.5965625" 
##  [6] "3.21725"    "17.84875"   "0.4375"     "0.40625"    "3.6875"    
## [11] "2.8125"
flights_typeof  <- c()
for (i in seq_along(flights)) {
  flights_typeof[[i]] <- typeof(flights[[i]])
}
flights_typeof
##  [1] "integer"   "integer"   "integer"   "integer"   "integer"  
##  [6] "double"    "integer"   "integer"   "double"    "character"
## [11] "integer"   "character" "character" "character" "double"   
## [16] "double"    "double"    "double"    "double"
iris_unique <- c()
for (i in seq_along(iris)) {
  iris_unique[[i]] <-
    length(unique(iris[[i]]))  # seq_along(unique(iris[[i]])) 차이점
}
iris_unique
## [1] 35 23 43 22  3

set.seed(1004)
df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10),
)
df
## # A tibble: 10 x 4
##          a      b      c      d
##      <dbl>  <dbl>  <dbl>  <dbl>
##  1 -0.608  -1.90  -0.747  0.916
##  2  0.768   1.13  -0.513  0.391
##  3 -0.164   0.149  0.871 -0.635
##  4 -0.0288 -0.215  0.831 -1.01 
##  5  0.0136  2.64  -1.03   0.356
##  6 -0.716  -0.268 -1.61   0.108
##  7 -1.48    1.12   2.17  -1.71 
##  8  0.828   0.484  0.672  0.685
##  9 -0.895   0.632  0.405 -0.608
## 10  1.88   -0.818 -0.832 -0.619
df_range <- c()
for (i in seq_along(df)) {
  df_range[[i]] <- range(df[[i]])
}
df_range     # list
## [[1]]
## [1] -1.477976  1.884156
## 
## [[2]]
## [1] -1.895257  2.637568
## 
## [[3]]
## [1] -1.614083  2.169269
## 
## [[4]]
## [1] -1.7124863  0.9159031
means <- c(0, 1, 2)
out <- c()
for (i in seq_along(means)) {
  n <- sample(100, 1)
  out[[i]] <- rnorm(n, means[[i]])
}
out
## [[1]]
##  [1]  0.88360673  1.23356277 -1.07503429 -0.50042731 -0.73641038
##  [6]  1.18207402  1.56307149 -1.19567022  0.01166787 -0.24062044
## [11]  1.15564152 -1.21649058 -0.87048902 -0.40080753 -0.37714742
## [16]  0.14684540  0.79268395 -0.90136571  1.08825190 -0.51823038
## [21]  0.26834363 -1.32133234  0.73030857
## 
## [[2]]
##  [1]  1.21502228  1.06521142  0.34143409  1.27607333  1.56874201
##  [6]  2.43930339  0.83882012  1.30583661  0.78673803 -0.89115214
## [11]  1.34094205  1.11326706  1.86763332  0.48700993  0.77491323
## [16]  2.49347924  2.67487262  3.24829071  0.20942913  0.54950268
## [21]  0.70957874 -0.11817998  1.27944272  2.30214919  2.26066396
## [26]  0.20396614 -0.02617654  0.42235140  0.02818222  1.62310427
## [31]  3.15364240 -0.19350394 -0.40757011  0.08232997  0.83831210
## [36] -0.01666443  0.55702273  0.61892275 -0.38618016  1.68976438
## [41] -1.60631757  0.17771851  1.08978289 -0.20268115  1.36286695
## [46]  1.53056756 -0.28001313  1.17451834  2.23297800  2.60937812
## [51] -1.48618529  0.29033673  1.06745076  1.30288935  1.47490096
## [56] -1.01716027  0.13476079  2.04117150  2.83326591  0.69922853
## [61]  0.40861884  1.35065559  2.06283348  0.53365160  0.33139703
## [66]  0.64337282  2.29249001  1.65803172  2.64970834 -0.19171864
## [71]  0.31554547  1.61730111  1.29156220 -0.34379510  0.86538631
## [76]  0.86361055
## 
## [[3]]
##  [1] 2.3113404 2.0036691 3.1043782 2.1843482 1.0039963 1.2542357 2.8647065
##  [8] 1.7808846 1.6185611 1.8631852 2.0928230 2.3538305 4.1875455 1.6152138
## [15] 1.7159346 0.6639264 3.8210651 3.1246075 0.6446374 2.4195687 0.4940433
## [22] 1.8152826 2.4048532

#입력시퀸스 길이를 알지 못할때 while loop 사용

set.seed(1004)
means <- c(0, 1, 2)
out <- c()

for (i in seq_along(means)) {
  n <- sample(100, 1)
  out[[i]] <- rnorm(n, means[[i]])
}
out
## [[1]]
##  [1] -0.68724006  2.03374857  1.36664847 -0.31656764 -0.18550908
##  [6] -0.35988952 -1.01880969 -1.83356276  1.39457219 -0.82707502
## [11]  0.72058771 -1.51082765  1.58677234 -0.33480301 -0.59797805
## [16]  0.05847549 -1.28581360 -0.10784110 -0.48980419 -0.72907722
## [21] -0.34414401  2.08394126  1.18407254  0.95354828 -1.31303430
## [26] -1.90130737 -0.81579540 -0.12949233
## 
## [[2]]
##  [1]  0.168320676  1.915903112  1.390553169  0.365273656 -0.006581307
##  [6]  1.356224626  1.108328431 -0.712486312  1.684619447  0.391695120
## [11]  0.380795667  0.232876703  1.363351813  0.267440272  2.230581162
## [16]  1.091652323  2.569938690  2.309472945  1.906978002 -0.372914956
## [21]  0.883648668  1.768525901  1.440285291 -0.900347712 -0.189728028
## [26]  0.573094272  0.594808168 -1.060563573  0.671099262  1.383963413
## [31]  0.333237951  1.054288991  1.172704508  1.825583138  1.056768726
## [36]  1.215022280  1.065211422  0.341434095  1.276073329  1.568742008
## [41]  2.439303390  0.838820115  1.305836614  0.786738029 -0.891152141
## [46]  1.340942050  1.113267061  1.867633320  0.487009927  0.774913233
## [51]  2.493479244  2.674872624  3.248290713  0.209429135  0.549502678
## 
## [[3]]
##  [1]  2.2983047  2.0043936  1.1596634  2.4268957  0.6935814 -0.2602925
##  [7]  4.0040734  2.9702139  1.9131742  3.4031912  3.0518967  1.1948501
## [13]  2.3207146  1.6671581  0.7111837  1.2878902  2.2725504  2.4719039
## [19] -0.3011137  1.5812468  2.8537139  1.8954133  1.4400749  2.8929650
## [25]  0.9119705  1.4541836  1.8011959  1.4342714  2.9138491  1.8390906
## [31]  1.9180419  2.7433308  2.7414788  2.5711364 -0.1468497  4.3015561
## [37]  1.7591142  1.5527960  2.0467581
unlist(out)
##   [1] -0.687240058  2.033748573  1.366648467 -0.316567641 -0.185509081
##   [6] -0.359889521 -1.018809690 -1.833562764  1.394572187 -0.827075019
##  [11]  0.720587714 -1.510827652  1.586772343 -0.334803014 -0.597978045
##  [16]  0.058475487 -1.285813597 -0.107841096 -0.489804185 -0.729077223
##  [21] -0.344144007  2.083941261  1.184072537  0.953548284 -1.313034303
##  [26] -1.901307366 -0.815795404 -0.129492333  0.168320676  1.915903112
##  [31]  1.390553169  0.365273656 -0.006581307  1.356224626  1.108328431
##  [36] -0.712486312  1.684619447  0.391695120  0.380795667  0.232876703
##  [41]  1.363351813  0.267440272  2.230581162  1.091652323  2.569938690
##  [46]  2.309472945  1.906978002 -0.372914956  0.883648668  1.768525901
##  [51]  1.440285291 -0.900347712 -0.189728028  0.573094272  0.594808168
##  [56] -1.060563573  0.671099262  1.383963413  0.333237951  1.054288991
##  [61]  1.172704508  1.825583138  1.056768726  1.215022280  1.065211422
##  [66]  0.341434095  1.276073329  1.568742008  2.439303390  0.838820115
##  [71]  1.305836614  0.786738029 -0.891152141  1.340942050  1.113267061
##  [76]  1.867633320  0.487009927  0.774913233  2.493479244  2.674872624
##  [81]  3.248290713  0.209429135  0.549502678  2.298304655  2.004393572
##  [86]  1.159663427  2.426895749  0.693581364 -0.260292514  4.004073431
##  [91]  2.970213860  1.913174226  3.403191242  3.051896667  1.194850129
##  [96]  2.320714553  1.667158065  0.711183678  1.287890229  2.272550396
## [101]  2.471903894 -0.301113718  1.581246829  2.853713927  1.895413300
## [106]  1.440074883  2.892964970  0.911970541  1.454183573  1.801195930
## [111]  1.434271425  2.913849134  1.839090635  1.918041863  2.743330843
## [116]  2.741478760  2.571136374 -0.146849722  4.301556146  1.759114156
## [121]  1.552796018  2.046758119
# 위와 같음
set.seed(1004)
means <- c(0, 1, 2)
out_w <- c()

i <- 1
while (i <= length(means)) {
  n <- sample(100, 1)
  out_w[[i]] <- rnorm(n, means[[i]])
  i <- i + 1
}
out_w
## [[1]]
##  [1] -0.68724006  2.03374857  1.36664847 -0.31656764 -0.18550908
##  [6] -0.35988952 -1.01880969 -1.83356276  1.39457219 -0.82707502
## [11]  0.72058771 -1.51082765  1.58677234 -0.33480301 -0.59797805
## [16]  0.05847549 -1.28581360 -0.10784110 -0.48980419 -0.72907722
## [21] -0.34414401  2.08394126  1.18407254  0.95354828 -1.31303430
## [26] -1.90130737 -0.81579540 -0.12949233
## 
## [[2]]
##  [1]  0.168320676  1.915903112  1.390553169  0.365273656 -0.006581307
##  [6]  1.356224626  1.108328431 -0.712486312  1.684619447  0.391695120
## [11]  0.380795667  0.232876703  1.363351813  0.267440272  2.230581162
## [16]  1.091652323  2.569938690  2.309472945  1.906978002 -0.372914956
## [21]  0.883648668  1.768525901  1.440285291 -0.900347712 -0.189728028
## [26]  0.573094272  0.594808168 -1.060563573  0.671099262  1.383963413
## [31]  0.333237951  1.054288991  1.172704508  1.825583138  1.056768726
## [36]  1.215022280  1.065211422  0.341434095  1.276073329  1.568742008
## [41]  2.439303390  0.838820115  1.305836614  0.786738029 -0.891152141
## [46]  1.340942050  1.113267061  1.867633320  0.487009927  0.774913233
## [51]  2.493479244  2.674872624  3.248290713  0.209429135  0.549502678
## 
## [[3]]
##  [1]  2.2983047  2.0043936  1.1596634  2.4268957  0.6935814 -0.2602925
##  [7]  4.0040734  2.9702139  1.9131742  3.4031912  3.0518967  1.1948501
## [13]  2.3207146  1.6671581  0.7111837  1.2878902  2.2725504  2.4719039
## [19] -0.3011137  1.5812468  2.8537139  1.8954133  1.4400749  2.8929650
## [25]  0.9119705  1.4541836  1.8011959  1.4342714  2.9138491  1.8390906
## [31]  1.9180419  2.7433308  2.7414788  2.5711364 -0.1468497  4.3015561
## [37]  1.7591142  1.5527960  2.0467581

out_w
## [[1]]
##  [1] -0.68724006  2.03374857  1.36664847 -0.31656764 -0.18550908
##  [6] -0.35988952 -1.01880969 -1.83356276  1.39457219 -0.82707502
## [11]  0.72058771 -1.51082765  1.58677234 -0.33480301 -0.59797805
## [16]  0.05847549 -1.28581360 -0.10784110 -0.48980419 -0.72907722
## [21] -0.34414401  2.08394126  1.18407254  0.95354828 -1.31303430
## [26] -1.90130737 -0.81579540 -0.12949233
## 
## [[2]]
##  [1]  0.168320676  1.915903112  1.390553169  0.365273656 -0.006581307
##  [6]  1.356224626  1.108328431 -0.712486312  1.684619447  0.391695120
## [11]  0.380795667  0.232876703  1.363351813  0.267440272  2.230581162
## [16]  1.091652323  2.569938690  2.309472945  1.906978002 -0.372914956
## [21]  0.883648668  1.768525901  1.440285291 -0.900347712 -0.189728028
## [26]  0.573094272  0.594808168 -1.060563573  0.671099262  1.383963413
## [31]  0.333237951  1.054288991  1.172704508  1.825583138  1.056768726
## [36]  1.215022280  1.065211422  0.341434095  1.276073329  1.568742008
## [41]  2.439303390  0.838820115  1.305836614  0.786738029 -0.891152141
## [46]  1.340942050  1.113267061  1.867633320  0.487009927  0.774913233
## [51]  2.493479244  2.674872624  3.248290713  0.209429135  0.549502678
## 
## [[3]]
##  [1]  2.2983047  2.0043936  1.1596634  2.4268957  0.6935814 -0.2602925
##  [7]  4.0040734  2.9702139  1.9131742  3.4031912  3.0518967  1.1948501
## [13]  2.3207146  1.6671581  0.7111837  1.2878902  2.2725504  2.4719039
## [19] -0.3011137  1.5812468  2.8537139  1.8954133  1.4400749  2.8929650
## [25]  0.9119705  1.4541836  1.8011959  1.4342714  2.9138491  1.8390906
## [31]  1.9180419  2.7433308  2.7414788  2.5711364 -0.1468497  4.3015561
## [37]  1.7591142  1.5527960  2.0467581
out_w_m <- c()
for (i in seq_along(out_w)) {
  out_w_m[[i]] <- mean(out_w[[i]])
}
out_w_m
## [1] -0.1216502  0.9991775  1.9434762
# 위와 같음
map_dbl(out_w, mean)
## [1] -0.1216502  0.9991775  1.9434762

col_summary <- function(df, fun) {
  out <- vector("double", length(df))
  for (i in seq_along(df)) {
    out[i] <- fun(df[[i]])
  }
  out
}
col_summary(df, mean)
## [1] -0.03964013  0.29560491  0.02077262 -0.21256744
col_summary(df, median)
## [1] -0.09652429  0.31682441 -0.05392461 -0.24998822