vectorization - Sum without use of loop in R -


i've vector.

basket <- c(4,5,10,102,10); 

if i've sum, call

sum(basket) 

if i've use loop find out sum,

total <- 0; len <- length(basket); for(i in 1:len) {total <- total + basket[i]} 

is there way find total, without using sum(), or using lengthy loop construct?

add <- function(x) reduce("+", x) add(basket) 

or write shorter loop:

s <- 0 for(a in basket) s<- s+a 

Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -