Dear all, just a stupid R question, since the results puzzle me a bit:> sum(c(NA,NA), na.rm=TRUE)[1] 0> NA + NA[1] NA> NA + 1[1] NA>Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? Thanks in advance, Will
On 15-Apr-10 12:37:42, Wilmar Igl wrote:> Dear all, > > just a stupid R question, since the results puzzle me a bit: > >> sum(c(NA,NA), na.rm=TRUE) > [1] 0 >> NA + NA > [1] NA >> NA + 1 > [1] NA >> > > Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? > > Thanks in advance, > > WillFor the same reason that: sum(logical(0)) # [1] 0 If x is a numeric vector, possibly with NAs, sum(x,na.rm=TRUE) will first remove any NAs from x, and then execute sum() on what is left. In the case of your example, after removing the NAs from c(NA,NA) there is nothing left. The fact that it comes out 'logical' is another issue: x <- c(NA,NA) x[!is.na(x)] # logical(0) c(NA,NA)[-(1:2)] # logical(0) while: c(3,4)[-(1:2)] numeric(0) The point is that the type of c(NA,NA) is "logical" str(c(NA,NA)) # logi [1:2] NA NA Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 15-Apr-10 Time: 14:07:10 ------------------------------ XFMail ------------------------------
On Apr 15, 2010, at 8:37 AM, Wilmar Igl wrote:> Dear all, > > just a stupid R question, since the results puzzle me a bit: > >> sum(c(NA,NA), na.rm=TRUE) > [1] 0 >> NA + NA > [1] NA >> NA + 1 > [1] NA >> > > Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? >> sum(c()) [1] 0 ?sum "NB: the sum of an empty set is zero, by definition." -- David Winsemius, MD West Hartford, CT
Dear all, just a stupid R question, since the results puzzle me a bit:> sum(c(NA,NA), na.rm=TRUE)[1] 0> NA + NA[1] NA> NA + 1[1] NA>Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? Thanks in advance, Will
So that the two lines below give the same answer: xx <- c(); yy <- 1:3 sum(xx) + sum(yy) sum(c(xx, yy)) On Tue, Apr 20, 2010 at 12:42 PM, <will.eagle at gmx.net> wrote:> Dear all, > > just a stupid R question, since the results puzzle me a bit: > >> sum(c(NA,NA), na.rm=TRUE) > [1] 0 >> ?NA + NA > [1] NA >> NA + 1 > [1] NA >> > > Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? > > Thanks in advance, > > Will > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
As a disclaimer, I cannot say that this is why sum() was designed as it was. 0 is the sum of a set with no elements, the empty set {}. When na.rm=TRUE, NA values are removed. When the only values are NA (as in your example c(NA, NA) ), and you remove them all, you are taking the sum of no elements, which is 0. Also note the behavior of sum() # returns 0 sum is one of the few functions that you can simply call that will not return an error. HTH, Josh On Tue, Apr 20, 2010 at 9:42 AM, <will.eagle at gmx.net> wrote:> Dear all, > > just a stupid R question, since the results puzzle me a bit: > >> sum(c(NA,NA), na.rm=TRUE) > [1] 0 >> ?NA + NA > [1] NA >> NA + 1 > [1] NA >> > > Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? > > Thanks in advance, > > Will > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/
On 20-Apr-10 17:07:31, Joshua Wiley wrote:> As a disclaimer, I cannot say that this is why sum() was designed as it > was. > > 0 is the sum of a set with no elements, the empty set {}. When > na.rm=TRUE, NA values are removed. When the only values are NA (as in > your example c(NA, NA) ), and you remove them all, you are taking the > sum of no elements, which is 0. > > Also note the behavior of > > sum() # returns 0 > > sum is one of the few functions that you can simply call that will not > return an error. > > HTH, > > Josh > > On Tue, Apr 20, 2010 at 9:42 AM, <will.eagle at gmx.net> wrote: >> Dear all, >> >> just a stupid R question, since the results puzzle me a bit: >> >>> sum(c(NA,NA), na.rm=TRUE) >> [1] 0 >>> _NA + NA >> [1] NA >>> NA + 1 >> [1] NA >>> >> >> Why does sum(c(NA,NA), na.rm=TRUE) return 0 and not NA? >> >> Thanks in advance, >> >> WillExactly the same question was asked, in exactly the same words, 5 days ago: https://stat.ethz.ch/pipermail/r-help/2010-April/235337.html and, I suspect, by exactly the same person (thoug using a different email address). And, also, it was answered 5 days ago. Was the second sending an accident? Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 20-Apr-10 Time: 18:24:38 ------------------------------ XFMail ------------------------------
>Exactly the same question was asked, in exactly the same words, 5 days ago: >https://stat.ethz.ch/pipermail/r-help/2010-April/235337.htmlDear all, sorry for reposting. I thought my first mail had been bounced off since I could not find it online and no answers on my email account. Problem solved now. Many thanks, Will