I am trying to use sum in a function, but the results are NA, which I think may be due to integer overflow. But the class of the numbers I am using is numeric.
The function is most simply
sum((columnA-columnB)^2) A value from columnA is 0.1376146 and from columnB is 0.272
Is is the different length of decimal places? I know how to change what is displayed, but I'm not sure that will change what R uses for sum.
1 Answer
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
NAs in your data? If you sum anything withNAs present, the result will beNA, unless you handle it properly.
That said, some solutions:
- Use
sum(..., na.rm=T)to ignoreNAs from your object (this is the simple solution) - Sum only non
NAentries: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)