simplest way to count number of ones in an array in R
r
Solution
If it is binary,
sum(!!myarray)
#[1] 3
Or
sum(myarray) #based on comments from @thelatemail
If not binary,
sum(myarray==1)
Problem
Is there a function which I can use to count the number of ones in an array in R? I was looking for something which would save me the overhead of ``` count = 0; myarray = c(1,1,0,1,0) for(i in 1:length(myarray)) { if(myarray[i] == 1) { count = count+1 } } ```