I use something like : dataframe[ is.na(dataframe) ] <- 0 dataframe[ is.nan(dataframe) ] <- 0 dataframe[ is.infinite(dataframe) ] <- 0 -----Original Message----- From: "Olu Ola via R-help" [r-help at r-project.org] Date: 09/06/2015 06:24 PM To: r-help at r-project.org Subject: [R] Handling "NA" in summation Hello, I am currently working with a dataframe which has some missing values represented by "NA". whenever, I add two columns in which at least one of the pair of an observation is "NA", the sum returns zero. That is for the same observation, if dataframe$A = 20 dataframe$B = NA dataframe$A + dataframe$B returns zero. I do not want to delete the observations with the NA's. How do I go about carrying out the necessary operations without deleting the observations with the NA's Thank you ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
I'm not quite sure how you get zero from that situation. Do you expect the answer to be 20? How about:> dataframe <- data.frame(A=20, B=NA)> dataframe$A + dataframe$B[1] NA> ?sum> sum(dataframe$A, dataframe$B, na.rm=TRUE)[1] 20 Sarah On Sun, Sep 6, 2015 at 6:48 PM, ce <zadig_1 at excite.com> wrote:> > > I use something like : > > dataframe[ is.na(dataframe) ] <- 0 > dataframe[ is.nan(dataframe) ] <- 0 > dataframe[ is.infinite(dataframe) ] <- 0 > > -----Original Message----- > From: "Olu Ola via R-help" [r-help at r-project.org] > Date: 09/06/2015 06:24 PM > To: r-help at r-project.org > Subject: [R] Handling "NA" in summation > > Hello, > I am currently working with a dataframe which has some missing values represented by "NA". whenever, I add two columns in which at least one of the pair of an observation is "NA", the sum returns zero. That is for the same observation, if > > dataframe$A = 20 > dataframe$B = NA > > dataframe$A + dataframe$B returns zero. > > I do not want to delete the observations with the NA's. How do I go about carrying out the necessary operations without deleting the observations with the NA's > > Thank you >-- Sarah Goslee http://www.functionaldiversity.org
So you have decided that NA==0 and Inf == 0... if that is really what you want then it looks like that is what you got. If you don't like the fact that you are mucking with your data, then make a copy of the data first and muck with that. Blegh. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 6, 2015 3:48:19 PM PDT, ce <zadig_1 at excite.com> wrote:> > >I use something like : > >dataframe[ is.na(dataframe) ] <- 0 >dataframe[ is.nan(dataframe) ] <- 0 >dataframe[ is.infinite(dataframe) ] <- 0 > >-----Original Message----- >From: "Olu Ola via R-help" [r-help at r-project.org] >Date: 09/06/2015 06:24 PM >To: r-help at r-project.org >Subject: [R] Handling "NA" in summation > >Hello, >I am currently working with a dataframe which has some missing values >represented by "NA". whenever, I add two columns in which at least one >of the pair of an observation is "NA", the sum returns zero. That is >for the same observation, if > >dataframe$A = 20 >dataframe$B = NA > >dataframe$A + dataframe$B returns zero. > >I do not want to delete the observations with the NA's. How do I go >about carrying out the necessary operations without deleting the >observations with the NA's > >Thank you > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.