Interpreting “condition has length > 1” warning from `if` function

后端 未结 6 1861
半阙折子戏
半阙折子戏 2020-11-22 10:31

I have an array:

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         


        
6条回答
  •  遇见更好的自我
    2020-11-22 11:15

    maybe you want ifelse:

    a <- c(1,1,1,1,0,0,0,0,2,2)
    ifelse(a>0,a/sum(a),1)
    
     [1] 0.125 0.125 0.125 0.125 1.000 1.000 1.000 1.000
     [9] 0.250 0.250
    

提交回复
热议问题