Dear list, how can I exclude the missing values in the following example: tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) I tried: tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum, Fun=is.na(lsk0t50$Pox2)) But it returns a lot of missing values in the result. Instead the missing values in the variable should not be taken into account. So that I get only a NA as result if there are only NAs for the corresponding factor. Any idea how this can be achieved? Thanks, Ulrich -- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31-(0)20-525-7456 (7451 Secretary) Fax: +31-(0)20-525-7431 Email: uleopold at science.uva.nl http://www.frw.uva.nl/soil/Welcome.html Check us also out at: Netherlands Centre for Geo-ecological Research http://www.frw.uva.nl/icg -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Barron wrote:> Try tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum,na.rm=TRUE)I tried but some strange things happen. The columns after Pox2 change into Pox2 and for some Nas I get now 0 as a result. How can I apply the above command to avoid mixing columns. The imported file was a tab seperated file. Ulrich> Dave > > ----- Original Message ----- > From: "Ulrich Leopold" <uleopold at science.uva.nl> > To: <r-help at stat.math.ethz.ch> > Sent: Thursday, June 27, 2002 7:56 AM > Subject: [R] missing values > > > >>Dear list, >> >>how can I exclude the missing values in the following example: >> >>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) >> >>I tried: >> >>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum, Fun=is.na(lsk0t50$Pox2)) >> >>But it returns a lot of missing values in the result. Instead the missing >>values in the variable should not be taken into account. So that I get > > only > >>a NA as result if there are only NAs for the corresponding factor. >> >>Any idea how this can be achieved? >> >>Thanks, >>Ulrich >>-- >>__________________________________________________ >> >>Ulrich Leopold MSc. >> >>Department of Physical Geography >>Institute for Biodiversity and Ecosystem Dynamics >>Faculty of Science >>University of Amsterdam >>Nieuwe Achtergracht 166 >>NL-1018WV Amsterdam >> >>Phone: +31-(0)20-525-7456 (7451 Secretary) >>Fax: +31-(0)20-525-7431 >>Email: uleopold at science.uva.nl >>http://www.frw.uva.nl/soil/Welcome.html >> >>Check us also out at: >>Netherlands Centre for Geo-ecological Research >>http://www.frw.uva.nl/icg >> >> >> >>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > > -.-.- > >>r-help mailing list -- Read > > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > >>Send "info", "help", or "[un]subscribe" >>(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >> > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._ > >> >-- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31-(0)20-525-7456 (7451 Secretary) Fax: +31-(0)20-525-7431 Email: uleopold at science.uva.nl http://www.frw.uva.nl/soil/Welcome.html Check us also out at: Netherlands Centre for Geo-ecological Research http://www.frw.uva.nl/icg -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> -----Original Message----- > From: Ulrich Leopold [mailto:uleopold at science.uva.nl] > Sent: Thursday, June 27, 2002 4:56 PM > To: r-help at stat.math.ethz.ch > Subject: [R] missing values > > Dear list, > > how can I exclude the missing values in the following example: > > tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) > > I tried: > > tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum, Fun=is.na(lsk0t50$Pox2))[WNV] If you use tapply(thevariable, thefactor, sum, na.rm = TRUE) you will *almost* get what you want.> But it returns a lot of missing values in the result. Instead the missing > values in the variable should not be taken into account. So that I get > only > a NA as result if there are only NAs for the corresponding factor.[WNV] If all values in the variable are missing and you remove them you get an empty sum, which is 0, not NA. So if you want this non-standard definition of the result of an empty sum you have to say so explicitly. One way to do it is as follows: mySpecialSum <- function(x) if(all(is.na(x)) || length(x) == 0) NA else sum(x, na.rm = TRUE) lapply(thevariable, thefactor, mySpecialSum) If you have missings in the factor (as you seem to say above) you are in a spot of bother. Bill Venables. ny idea how this can be achieved?> Thanks, > Ulrich > -- > __________________________________________________ > > Ulrich Leopold MSc. > > Department of Physical Geography > Institute for Biodiversity and Ecosystem Dynamics > Faculty of Science > University of Amsterdam > Nieuwe Achtergracht 166 > NL-1018WV Amsterdam > > Phone: +31-(0)20-525-7456 (7451 Secretary) > Fax: +31-(0)20-525-7431 > Email: uleopold at science.uva.nl > http://www.frw.uva.nl/soil/Welcome.html > > Check us also out at: > Netherlands Centre for Geo-ecological Research > http://www.frw.uva.nl/icg > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.- > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ulrich Leopold wrote:> how can I exclude the missing values in the following example: > > tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum)tapply(lsk0t50$Pox2, lsk0t50$ProfN, function(x){sum(na.omit(x))}) Hope it helps, Chuck -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 27 Jun 2002, Ulrich Leopold wrote: |Dear list, | |how can I exclude the missing values in the following example: | |tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) You should try tapply(......, na.rm=TRUE) look ?sum and ?tapply Ott -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Renaud Lancelot wrote:> tapply(lsk0t50$Pox2, lsk0t50$ProfN, function(x) sum(x[!is.na(x)]) )Also for your suggestion I get the same strange result. It seems that the following columns interfere with the variable(Pox2) column because the missing values are omitted, excluded etc. So maybe I am using the wrong command by using tapply? I have a data object with several columns and would like to sum one column by a factor given in antother column but omitting, excluding the NAs. Regards, Ulrich> Best, > > Renaud > > Ulrich Leopold wrote: > >>Dear list, >> >>how can I exclude the missing values in the following example: >> >>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) >> >>I tried: >> >>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum, Fun=is.na(lsk0t50$Pox2)) >> >>But it returns a lot of missing values in the result. Instead the missing >>values in the variable should not be taken into account. So that I get only >>a NA as result if there are only NAs for the corresponding factor. >> >>Any idea how this can be achieved? >> >>Thanks, >>Ulrich >>-- >>__________________________________________________ >> >>Ulrich Leopold MSc. >> >>Department of Physical Geography >>Institute for Biodiversity and Ecosystem Dynamics >>Faculty of Science >>University of Amsterdam >>Nieuwe Achtergracht 166 >>NL-1018WV Amsterdam >> >>Phone: +31-(0)20-525-7456 (7451 Secretary) >>Fax: +31-(0)20-525-7431 >>Email: uleopold at science.uva.nl >>http://www.frw.uva.nl/soil/Welcome.html >> >>Check us also out at: >>Netherlands Centre for Geo-ecological Research >>http://www.frw.uva.nl/icg >> >>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >>Send "info", "help", or "[un]subscribe" >>(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > >-- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31-(0)20-525-7456 (7451 Secretary) Fax: +31-(0)20-525-7431 Email: uleopold at science.uva.nl http://www.frw.uva.nl/soil/Welcome.html Check us also out at: Netherlands Centre for Geo-ecological Research http://www.frw.uva.nl/icg -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Renaud Lancelot wrote:> maybe you have missing values in lsk0t50$Pox2 ? Try: > > cond <- !is.na(lsk0t50$Pox2) > tapply(lsk0t50$Pox2[cond], lsk0t50$ProfN[cond], function(x) > sum(x[!is.na(x)]) )Yes this works fine. Only the NAs are now also excluded from the results. How can I also return the missing values in the results as NAs? Regards, Ulrich> Best, > > Renaud > > > tapply(lsk0t50$Pox2, lsk0t50$ProfN, function(x) sum(x[!is.na(x)]) ) > > Ulrich Leopold wrote: > >>Renaud Lancelot wrote: >> >>>tapply(lsk0t50$Pox2, lsk0t50$ProfN, function(x) sum(x[!is.na(x)]) ) >> >>Also for your suggestion I get the same strange result. It seems that the >>following columns interfere with the variable(Pox2) column because the >>missing values are omitted, excluded etc. >> >>So maybe I am using the wrong command by using tapply? >> >>I have a data object with several columns and would like to sum one column >>by a factor given in antother column but omitting, excluding the NAs. >> >>Regards, Ulrich >> >> >>>Best, >>> >>>Renaud >>> >>>Ulrich Leopold wrote: >>> >>> >>>>Dear list, >>>> >>>>how can I exclude the missing values in the following example: >>>> >>>>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum) >>>> >>>>I tried: >>>> >>>>tapply(lsk0t50$Pox2, lsk0t50$ProfN, sum, Fun=is.na(lsk0t50$Pox2)) >>>> >>>>But it returns a lot of missing values in the result. Instead the missing >>>>values in the variable should not be taken into account. So that I get only >>>>a NA as result if there are only NAs for the corresponding factor. >>>> >>>>Any idea how this can be achieved? >>>> >>>>Thanks, >>>>Ulrich >>>>-- >>>>__________________________________________________ >>>> >>>>Ulrich Leopold MSc. >>>> >>>>Department of Physical Geography >>>>Institute for Biodiversity and Ecosystem Dynamics >>>>Faculty of Science >>>>University of Amsterdam >>>>Nieuwe Achtergracht 166 >>>>NL-1018WV Amsterdam >>>> >>>>Phone: +31-(0)20-525-7456 (7451 Secretary) >>>>Fax: +31-(0)20-525-7431 >>>>Email: uleopold at science.uva.nl >>>>http://www.frw.uva.nl/soil/Welcome.html >>>> >>>>Check us also out at: >>>>Netherlands Centre for Geo-ecological Research >>>>http://www.frw.uva.nl/icg >>>> >>>>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >>>>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >>>>Send "info", "help", or "[un]subscribe" >>>>(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >>>>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >>> >>> >>-- >>__________________________________________________ >> >>Ulrich Leopold MSc. >> >>Department of Physical Geography >>Institute for Biodiversity and Ecosystem Dynamics >>Faculty of Science >>University of Amsterdam >>Nieuwe Achtergracht 166 >>NL-1018WV Amsterdam >> >>Phone: +31-(0)20-525-7456 (7451 Secretary) >>Fax: +31-(0)20-525-7431 >>Email: uleopold at science.uva.nl >>http://www.frw.uva.nl/soil/Welcome.html >> >>Check us also out at: >>Netherlands Centre for Geo-ecological Research >>http://www.frw.uva.nl/icg > >-- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31-(0)20-525-7456 (7451 Secretary) Fax: +31-(0)20-525-7431 Email: uleopold at science.uva.nl http://www.frw.uva.nl/soil/Welcome.html Check us also out at: Netherlands Centre for Geo-ecological Research http://www.frw.uva.nl/icg -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>gsum <- function(v1, v2){ > > + cond <- is.na(v1) > + res1 <- tapply(v1[!cond], v2[!cond], function(x) sum(x[!is.na(x)])) > + res2 <- tapply(v1, v2, function(x) sum(is.na(x))) > + dat <- as.data.frame(rbind(table(v2), res1, res2)) > + dimnames(dat)[[1]] <- c("Freq", "Sum", "Missing") > + cat("Missing in", deparse(substitute(v1)), ":", sum(cond), "\n") > + dat > + }This works in the same way as before... NAs will be zeros. I think it is rather a problem of sum. If I have only NAs sum returns 0. So maybe sum starts as initial value at 0 and then adds the following values. Could this be the reason? If yes. Can I tell sum: do not sum if there is a missing value? na.rm, is.na, na.omit do not work Regards, Ulrich -- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31-(0)20-525-7456 (7451 Secretary) Fax: +31-(0)20-525-7431 Email: uleopold at science.uva.nl http://www.frw.uva.nl/soil/Welcome.html Check us also out at: Netherlands Centre for Geo-ecological Research http://www.frw.uva.nl/icg -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._