我有一个数组:
a <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
并希望实现以下功能:
w<-function(a){
if (a>0){
a/sum(a)
}
else 1
}
此函数想检查是否有任何a
大于 0 的值,如果是,则将每个元素除以总数之和。
否则它应该只记录 1。
我收到以下警告消息:
Warning message:
In if (a > 0) { :
the condition has length > 1 and only the first element will be used
如何更正该功能?