Zhiguang Huo (Caleb)
Wednesday September 9, 2020
a <- 1:1000000
### version A, loop
start <- Sys.time()
meanA <- 0
for(i in seq_along(a)){
meanA <- meanA + a[i]/length(a)
}
end <- Sys.time()
end - start
## Time difference of 0.2877691 secs
## [1] 500000.5
## [1] 500000.5
## Time difference of 0.006999969 secs
a <- 1:1000000
b <- 1000000:1
### version A, loop
start <- Sys.time()
result <- numeric(length(a)) ## create a vector with length length(a) and all elements 0
for(i in seq_along(a)){
result[i] <- a[i] + b[i]
}
end <- Sys.time()
end - start
## Time difference of 0.1359711 secs
## Time difference of 0.02856016 secs
## [1] 10.1 5.4 10.3
## [1] 9.9 6.8 7.6
## [1] 2.357948 11.560000 6.055903
## [1] 3 4 5 6 7 8 9 10
## [1] 1 3 3 5 5 7 7 9
## Warning in a + c(1, 2, 3): longer object length is not a multiple of
## shorter object length
## [1] 2 4 6 5 7 9 8 10
## [1] 0.8414710 0.9092974 0.1411200
## [1] 1.5574077 -2.1850399 -0.1425465
## [1] 0.0000000 0.3010300 0.4771213
## [1] 0.0000000 0.3010300 0.4771213
## [1] 2.718282 7.389056 20.085537
a <- seq(1,8,1)
## by loop
res <- character(length(a))
for(i in 1:length(a)){
if(a[i] %% 2 == 0){
res[i] <- "even"
} else{
res[i] <- "odd"
}
}
res
## [1] "odd" "even" "odd" "even" "odd" "even" "odd" "even"
## [1] "odd" "even" "odd" "even" "odd" "even" "odd" "even"
## [1] 0 0 0 0 0
## [1] 0 0 0 0 0
## [1] 0 0 0 0 0
## [1] 0 0 0 0 0
## [1] "" "" "" "" ""
## [1] FALSE FALSE FALSE FALSE FALSE
## [1] 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
## [18] 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76
## [35] 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 10 20 30 40 50 60 70 80 90 100
## [1] 10 20 30 40 50 60 70 80 90 100
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 1 2 3 4 5 6 7 8 9 10
set.seed(32611) ## set a seed number such that the random numbers will keep the same
rnorm(n = 5, mean = 0, sd = 1)
## [1] -0.1023726 -1.8869039 -1.0586563 1.0227599 0.3982516
## [1] -0.1023726 -0.8869039 0.9413437 4.0227599 4.3982516
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS 10.15.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_3.6.0 magrittr_1.5 tools_3.6.0 htmltools_0.4.0
## [5] yaml_2.2.0 Rcpp_1.0.5 stringi_1.4.3 rmarkdown_1.13
## [9] knitr_1.23 stringr_1.4.0 xfun_0.7 digest_0.6.19
## [13] rlang_0.4.6 evaluate_0.13
## [1] 1 7 3
## [1] 1 7 3 4 5 6 10 2 8 9
## [1] 3 8 9 7 2 6 5 5 7 3
## [1] 0.45923048 0.37486058 0.02958663 0.51775341
## [[1]]
## list()
##
## [[2]]
## list()
##
## [[3]]
## list()
##
## [[4]]
## list()
## [[1]]
## [1] 0.3748606
##
## [[2]]
## [1] 0.51775341 0.14487818 0.02609954
##
## [[3]]
## [1] 0.6547776 0.5071504 0.2504906 0.9807351 0.4424658
##
## [[4]]
## [1] 0.5409666 0.5041831 0.6529512 0.7552580 0.5850660
##
## [[5]]
## [1] 0.2715686 0.3032975
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
b <- matrix(6:1, nrow=3, ncol=2)
a * b ## similar to vector, matrix algebra will be done element-wise.
## [,1] [,2]
## [1,] 6 12
## [2,] 10 10
## [3,] 12 6
## [,1] [,2]
## [1,] 2 5
## [2,] 4 7
## [3,] 6 9
## [,1] [,2]
## [1,] 4 7
## [2,] 5 8
## [3,] 6 9
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [1] 5 7 9
## [1] 5 7 9
## [1] 4.5 4.5 4.5
## [,1] [,2] [,3]
## [1,] 1 4 9
## [2,] 16 25 36
## [1] 6 15
## [1] 6 15
## [1] 2 5
## [1] 1 4
## [1] 1 4
## [,1] [,2] [,3]
## [1,] -0.4094903 -4.234625 1.593006
## [2,] -7.5476157 4.091040 -2.691786
## [,1] [,2] [,3]
## [1,] 3.825135 0.00000 5.827631
## [2,] 0.000000 11.63866 4.855829
## [,1] [,2] [,3]
## [1,] 0.656379 0 1.0000000
## [2,] 0.000000 1 0.4172157
l <- list(c(1:10), list(a='a'), c("dog","cat","gator"))
unlist(lapply(l, function(x) x[1])) ## x refers to each element of the list
## a
## "1" "a" "dog"
## [1] 10 1 3
lapply2 <- function(x, f, ...){
out <- vector("list", length(x))
for(i in seq_along(x)){
out[[i]] <- f(x[[i]], ...)
}
out
}
unlist(lapply2(l, length))
## [1] 10 1 3
## $a
## [1] 5
##
## $b
## [1] 10
##
## $c
## [1] 8
## [[1]]
## [1] 5
##
## [[2]]
## [1] 10
##
## [[3]]
## [1] 8
## [[1]]
## [1] 5
##
## [[2]]
## [1] 10
##
## [[3]]
## [1] 8
## $col1
## [1] 2
##
## $col2
## [1] 5
## col1 col2
## 2 5
compute_mean <- list(
base = function(x) mean(x),
sum = function(x) sum(x)/length(x),
mannual = function(x){
total <- 0; n <- length(x)
for(i in seq_along(x)) total <- total + x[i]/n
total
}
)
set.seed(32611); x <- runif(1e6)
lapply(compute_mean,function(f) system.time(f(x))) ## f refers to each element of the list
## $base
## user system elapsed
## 0.003 0.000 0.003
##
## $sum
## user system elapsed
## 0.001 0.000 0.001
##
## $mannual
## user system elapsed
## 0.058 0.000 0.058
## col1 col2
## 6 15
alist <- list(col1=1:3,col2=c("a","b"))
sapply(alist, unique) ## if not the same type, will coerce to a list
## $col1
## [1] 1 2 3
##
## $col2
## [1] "a" "b"
## col1 col2
## 6 15
pulse <- round(rnorm(22,70,10/3) + rep(c(0,5), c(10,12)))
groups <- rep(c("A", "B"), c(10, 12))
tapply(X = pulse, INDEX = groups, FUN = length)
## A B
## 10 12
## A B
## 70.20000 74.91667
tapply2 <- function(x, group, f, ..., simplify = TRUE){
pieces <- split(x, group)
sapply(pieces, f, simplify=simplify)
}
tapply2(pulse, groups, length)
## A B
## 10 12
xs <- replicate(3, runif(4),simplify=FALSE) ## simplify = TRUE (default) will convert a list to matrix whenever possible
ws <- replicate(3, rnorm(4, 1) + 1,simplify=FALSE)
xs
## [[1]]
## [1] 0.08032161 0.79005156 0.60823794 0.43907623
##
## [[2]]
## [1] 0.2121942 0.7209280 0.3716162 0.7708587
##
## [[3]]
## [1] 0.7943670 0.3145729 0.4271521 0.3151103
## [[1]]
## [1] 2.6430216 0.2654494 2.0656753 2.0416840
##
## [[2]]
## [1] 1.378534 1.673015 1.873283 3.820249
##
## [[3]]
## [1] 2.394193 1.196721 2.730337 1.249842
## [1] 0.3670111 0.5877195 0.5069851
## [[1]]
## [1] 0.3670111
##
## [[2]]
## [1] 0.5877195
##
## [[3]]
## [1] 0.5069851
## [[1]]
## [1] 0.3670111
##
## [[2]]
## [1] 0.5877195
##
## [[3]]
## [1] 0.5069851
## [[1]]
## [1] 0.3670111
##
## [[2]]
## [1] 0.5877195
##
## [[3]]
## [1] 0.5069851
## [1] 0.3670111 0.5877195 0.5069851
## [1] 0.3670111 0.5877195 0.5069851
## List of 4
## $ : int [1:15] 1 7 3 7 8 5 5 1 2 6 ...
## $ : int [1:15] 6 5 5 7 3 7 3 1 10 7 ...
## $ : int [1:15] 5 1 10 6 4 5 10 7 8 1 ...
## $ : int [1:15] 1 1 4 4 2 7 3 10 4 2 ...
## [1] 1 7 5 2
## [1] 1 7 5 2
## [1] 55
## [1] "abcdefghij"
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 2 4 6 8 10
## [3,] 3 6 9 12 15
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 2 4 6 8 10
## [3,] 3 6 9 12 15
## [,1] [,2] [,3] [,4] [,5]
## [1,] "1 1" "1 2" "1 3" "1 4" "1 5"
## [2,] "2 1" "2 2" "2 3" "2 4" "2 5"
## [3,] "3 1" "3 2" "3 3" "3 4" "3 5"
## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE FALSE FALSE
## [3,] TRUE TRUE FALSE FALSE FALSE
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 1 1 1 1
## [2,] 2 4 8 16 32
## [3,] 3 9 27 81 243
f <- function(x, y) c(x, y)
vf <- Vectorize(f, vectorize.args = c("x", "y"), SIMPLIFY = FALSE)
f(1:3, 1:3)
## [1] 1 2 3 1 2 3
## [[1]]
## [1] 1 1
##
## [[2]]
## [1] 2 2
##
## [[3]]
## [1] 3 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 2 2 3
## [2,] 2 3 4 3 4 4
## However, you cannot do combn(c(4,5),c(2,3)), how to vectorize this function
combnV <- Vectorize(function(x, m) combn(x, m),
vectorize.args = c("x", "m"))
combnV(c(4,5),c(2,3))
## [[1]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 2 2 3
## [2,] 2 3 4 3 4 4
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 1 1 1 1 1 2 2 2 3
## [2,] 2 2 2 3 3 4 3 3 4 4
## [3,] 3 4 5 4 5 5 4 5 5 5