Reputation: 41
I wrote following R code to count the odd numbers:
abd<- function (x) {
k<-0
for (n in x) {
if (n%%2==1) k <- k+1
}
return k
}
But it keep give me unused arguments error whenever i try with string of numbers like abd(c(1,2,3)) Anyone could help?
Upvotes: 0
Views: 1814
Reputation: 6542
to count the odd numbers, you could do :
number <- 1:10
number
#> [1] 1 2 3 4 5 6 7 8 9 10
sum(number %% 2)
#> [1] 5
Upvotes: 1