[问题类型]:
程式码咨询
[软件熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
答案是 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