EX1
# 本題欲了解下列function之功能,先執行
lapply(lapply(search(), ls), length)
## [[1]]
## [1] 0
##
## [[2]]
## [1] 153
##
## [[3]]
## [1] 13
##
## [[4]]
## [1] 27
##
## [[5]]
## [1] 149
##
## [[6]]
## [1] 403
##
## [[7]]
## [1] 237
##
## [[8]]
## [1] 10
##
## [[9]]
## [1] 42
##
## [[10]]
## [1] 447
##
## [[11]]
## [1] 87
##
## [[12]]
## [1] 108
##
## [[13]]
## [1] 241
##
## [[14]]
## [1] 104
##
## [[15]]
## [1] 218
##
## [[16]]
## [1] 0
##
## [[17]]
## [1] 1220
# 從結果觀察,第一層lapply應為顯示出search()所找尋到物件的數量,第二層應為該物件數量之長度
EX2
functionP <- function(rate, loan, year){
month <- 12*year
pay <- outer(loan, (rate/(1-outer((1+rate),(-month), "^"))), "*")
return(pay)
}
functionP(year = seq(10, 30, 5),
loan = c(5000000, 10000000, 15000000, 20000000),
rate = c(0.02, 0.05, 0.07, 0.09))
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 110240.5 250718.6 350104.3 450014.5
## [2,] 220481.0 501437.1 700208.5 900029.0
## [3,] 330721.5 752155.7 1050312.8 1350043.6
## [4,] 440961.9 1002874.3 1400417.1 1800058.1
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 102913.7 250038.4 350001.8 450000.1
## [2,] 205827.4 500076.7 700003.6 900000.2
## [3,] 308741.0 750115.1 1050005.4 1350000.2
## [4,] 411654.7 1000153.5 1400007.2 1800000.3
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 100870.4 250002.1 350000.0 450000
## [2,] 201740.8 500004.1 700000.1 900000
## [3,] 302611.2 750006.2 1050000.1 1350000
## [4,] 403481.6 1000008.2 1400000.1 1800000
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 100263.7 250000.1 350000 450000
## [2,] 200527.4 500000.2 700000 900000
## [3,] 300791.1 750000.3 1050000 1350000
## [4,] 401054.8 1000000.4 1400000 1800000
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 100080.2 250000 350000 450000
## [2,] 200160.4 500000 700000 900000
## [3,] 300240.7 750000 1050000 1350000
## [4,] 400320.9 1000000 1400000 1800000
EX3
library(magrittr)
## Warning: package 'magrittr' was built under R version 3.4.3
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
dta3 <- read.table("hs0.txt", header = TRUE)
# (a)
mapply(function(x,y){
t.test(dta3[, x], dta3[ ,y])$p.value
}, x = rep(7:11, each = 5), y = 7:11) %>%
matrix(5, 5)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0000000 0.5812665 0.6728327 0.7572410 0.8676815
## [2,] 0.5812665 1.0000000 0.8903494 0.3775863 0.7150322
## [3,] 0.6728327 0.8903494 1.0000000 0.4515844 0.8118467
## [4,] 0.7572410 0.3775863 0.4515844 1.0000000 0.6377587
## [5,] 0.8676815 0.7150322 0.8118467 0.6377587 1.0000000
# (b)
yvar <- names(dta3)[7:11]
lapply(yvar, function(y) {
oneway.test(substitute(i ~ race, list(i = as.name(y))), data = dta3)$p.value}) %>%
unlist()
## [1] 8.794587e-04 3.045127e-04 6.490966e-05 1.217540e-05 4.209418e-02
# (c)
lm(math ~ -1 + ses, data = dta3)
##
## Call:
## lm(formula = math ~ -1 + ses, data = dta3)
##
## Coefficients:
## seshigh seslow sesmiddle
## 56.17 49.17 52.21
ggplot(dta3, aes(ses, math))+
stat_summary(fun.data = mean_cl_boot, na.rm = TRUE)+
scale_x_discrete(limits = c("low", "middle", "high"))+
scale_y_continuous(breaks = seq(40, 70, by = 1))+
labs(x = "SES", y = "Average Math Score")

EX4
fcircle <- function(x) {sqrt(1^2 - x^2)}
N <- 10000
sum(fcircle(runif(N, 0, 1)) > runif(N, 0, 1)) / N * (1 * 1)
## [1] 0.7856