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

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -