1000개의 무작위 균일 난수를 입력으로 제공받은 함수를 호출
l = replicate(20, rnorm(sample(1:10, 1), mean=0, sd=1), simplify=FALSE)
out = vector("list", length(l))
for (i in seq(l)) {
out[[i]] = length(l[[i]])
}
unlist(out)
## [1] 5 4 5 9 10 3 5 3 3 9 1 10 9 5 8 9 2 9 10 9
unlist(lapply(l, length))
## [1] 5 4 5 9 10 3 5 3 3 9 1 10 9 5 8 9 2 9 10 9
mtcars[] = lapply(mtcars, function(x){
x/mean(x)
})
xs = runif(1e4)
res = c()
for (x in xs) {
res = c(res, sqrt(x))
}
res = numeric(length(xs))
for (i in seq(xs)) {
res[i] = sqrt(xs[i])
}
xs = runif(1e4)
ret = unlist(lapply(xs, function(x){
sqrt(x)
}))
ret[1:10]
## [1] 0.4223976 0.5768065 0.9410556 0.2452229 0.8626159 0.9631699 0.5534060
## [8] 0.6318466 0.8488031 0.8736057
res = numeric(length(xs))
ret = unlist(lapply(seq(xs), function(i){
res[i] = sqrt(xs[i])
}))
ret[1:10]
## [1] 0.4223976 0.5768065 0.9410556 0.2452229 0.8626159 0.9631699 0.5534060
## [8] 0.6318466 0.8488031 0.8736057