Germán Bonilla
2009-Jun-17 01:56 UTC
[R] wrong results when apply+sum with missing values (NA)
Hi all, I can't compute the sum by columns to a matrix using apply() when I've got missing values (either 0 or NA)... I've got this matrix:>nafamMicro Flavo Helio Pseud Rhodo Bdello Chloro Syntro Verruco Prochloro SAR11 I 1 1 1 NA NA 1 3 NA 1 6 1 VII NA 1 2 1 NA 2 16 1 NA 12 35 XI NA NA NA NA 1 NA 2 NA 1 17 94 and want to count the total frequencies by column, but I keep getting the same output>apply(nafam,2,sum,na.nr=T)Micro Flavo Helio Pseud Rhodo Bdello Chloro Syntro NA NA NA NA NA NA 22 NA Verruco Prochloro SAR11 NA 36 131 or this output with this other way:> apply(nafam,2,sum,na.last=NA)Micro Flavo Helio Pseud Rhodo Bdello Chloro Syntro NA NA NA NA NA NA NA NA Verruco Prochloro SAR11 NA NA NA I tried to change NA's to "0" and built this matrix:> nafam[is.na(nafam)] <- 0 > nafamMicro Flavo Helio Pseud Rhodo Bdello Chloro Syntro Verruco Prochloro SAR11 I 1 1 1 0 0 1 3 0 1 6 1 VII 0 1 2 1 0 2 16 1 0 12 35 XI 0 0 0 0 1 0 2 0 1 17 94 and then tried to sum again everything but the zeroes:> apply(tfam>0,2,sum)Micro Flavo Helio Pseud Rhodo Bdello Chloro Syntro 1 2 2 1 1 2 3 1 Verruco Prochloro SAR11 2 3 3 but the sums doesn't add correctly!!! (see Helio = 2 when it should be 3) I do need to exclude the zeroes because I'm using the output fro plotting a cumulative frequency distribution. Any idea of why is this happening? [[alternative HTML version deleted]]
Rolf Turner
2009-Jun-17 02:05 UTC
[R] wrong results when apply+sum with missing values (NA)
Where the <expletive deleted> did you get the argument ``na.nr''? Perhaps you need new reading glasses. Anyhow, unless there's something else you're not telling us, apply(nafam,2,sum,na.rm=TRUE) should work just fine. cheers, Rolf Turner On 17/06/2009, at 1:56 PM, Germ?n Bonilla wrote:> Hi all, > > I can't compute the sum by columns to a matrix using apply() when > I've got > missing values (either 0 or NA)... > I've got this matrix: > >> nafam > > Micro Flavo Helio Pseud Rhodo Bdello Chloro Syntro Verruco > Prochloro > SAR11 > I 1 1 1 NA NA 1 3 NA > 1 6 > 1 > VII NA 1 2 1 NA 2 16 1 > NA 12 > 35 > XI NA NA NA NA 1 NA 2 NA > 1 17 > 94 > > and want to count the total frequencies by column, but I keep > getting the > same output > >> apply(nafam,2,sum,na.nr=T) > > Micro Flavo Helio Pseud Rhodo Bdello Chloro > Syntro > NA NA NA NA NA NA 22 > NA > Verruco Prochloro SAR11 > NA 36 131 > > or this output with this other way: > >> apply(nafam,2,sum,na.last=NA) > Micro Flavo Helio Pseud Rhodo Bdello Chloro > Syntro > NA NA NA NA NA NA NA > NA > Verruco Prochloro SAR11 > NA NA NA > > I tried to change NA's to "0" and built this matrix: > >> nafam[is.na(nafam)] <- 0 >> nafam > > Micro Flavo Helio Pseud Rhodo Bdello Chloro Syntro Verruco > Prochloro > SAR11 > I 1 1 1 0 0 1 3 0 > 1 6 > 1 > VII 0 1 2 1 0 2 16 1 > 0 12 > 35 > XI 0 0 0 0 1 0 2 0 > 1 17 > 94 > > and then tried to sum again everything but the zeroes: > >> apply(tfam>0,2,sum) > Micro Flavo Helio Pseud Rhodo Bdello Chloro > Syntro > 1 2 2 1 1 2 > 3 1 > Verruco Prochloro SAR11 > 2 3 3 > > but the sums doesn't add correctly!!! (see Helio = 2 when it should > be 3) > I do need to exclude the zeroes because I'm using the output fro > plotting a > cumulative frequency distribution. > > Any idea of why is this happening? > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################