Following Joshua Ulrich's comment, before saying that you have some overflow problem, you should answer these questions:
- How many elements are you summing? R can handle a BIG number of entries
- How big are the values in your vectors? Again, R can handle quite big numbers
- Are you summing integers or floats? If you are summing floating-point numbers, you can't have an integer overflow (floats are not integers)
- Do you have
NA
s in your data? If you sum anything with NA
s present, the result will be NA
, unless you handle it properly.
That said, some solutions:
- Use
sum(..., na.rm=T)
to ignore NA
s from your object (this is the simple solution)
- Sum only non
NA
entries: sum(yourVector[!is.na(yourVector)]
(the not so simple one)
- If you are summing a column from a data frame, subset the data frame before summing:
sum(subset(yourDataFrame, !is.na(columnToSum))[columnToSum])
(this is like using a cannon to kill a mosquito)