rdevn00b
rdevn00b

Reputation: 562

Strange sum behavior in R

Can someone tell me whats going on here?

> check
[1] 1 5 4 6 7
> sum(check[1]:check[4])
[1] 21

Shouldn't this sum to 16?

Upvotes: 0

Views: 68

Answers (1)

IRTFM
IRTFM

Reputation: 263301

You asked for sum(1:6). If you wanted the sum of the first 4 items in check it would be:

sum( check[1:4] )

The ":" function first evaluates its flanking arguments, in your case finding 1 and 6 and then returns that sequence.

Upvotes: 6

Related Questions