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]]
______________________________________________
[email protected] 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.