[問題類型]:
程式碼諮詢
[軟體熟悉度]:
入門(寫過其他程式,只是對語法不熟悉)
[問題敘述]:
答案是 1101,主要的原因是不知道apply後面參數的3和rw是代表什麼意思,是c=3,rw=x矩
陣嗎?那第二段程式碼sum(rw[1:c]) / c 是把1:c列的數字加總再除以c?
[程式範例]:
copymaj <- function(rw,c) {
maj <- sum(rw[1:c]) / c
return(ifelse(maj > 0.5,1,0))
}
(x <- matrix(c(1,1,1,0,0,1,0,1,1,1,0,1,1,1,1,1,0,0,1,0),nrow = 4))
apply(x,1,copymaj,3)
[環境敘述]:
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
[2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1