Hi, how can I replace NA value with 0: 1991 217 119 103 109 137 202 283 240 146 NA 1992 270 174 149 144 166 239 278 237 275 NA 1993 146 111 104 89 98 131 153 148 175 NA 1994 177 123 146 124 121 200 266 191 240 106 1995 145 98 95 89 95 130 183 161 164 129 1996 145 98 89 90 93 138 158 131 161 161 1991 217 119 103 109 137 202 283 240 146 0 1992 270 174 149 144 166 239 278 237 275 0 1993 146 111 104 89 98 131 153 148 175 0 1994 177 123 146 124 121 200 266 191 240 106 1995 145 98 95 89 95 130 183 161 164 129 1996 145 98 89 90 93 138 158 131 161 161 Best wishes, Alfredo [[alternative HTML version deleted]]
x[ is.na(x) ] <- 0 should work in most cases i think. Gabor On Fri, Sep 14, 2007 at 10:08:19AM +0200, Alfredo Alessandrini wrote:> Hi, > > how can I replace NA value with 0: > > 1991 217 119 103 109 137 202 283 240 146 NA > 1992 270 174 149 144 166 239 278 237 275 NA > 1993 146 111 104 89 98 131 153 148 175 NA > 1994 177 123 146 124 121 200 266 191 240 106 > 1995 145 98 95 89 95 130 183 161 164 129 > 1996 145 98 89 90 93 138 158 131 161 161 > > 1991 217 119 103 109 137 202 283 240 146 0 > 1992 270 174 149 144 166 239 278 237 275 0 > 1993 146 111 104 89 98 131 153 148 175 0 > 1994 177 123 146 124 121 200 266 191 240 106 > 1995 145 98 95 89 95 130 183 161 164 129 > 1996 145 98 89 90 93 138 158 131 161 161 > > > Best wishes, > > Alfredo > > [[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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
>>> Gabor Csardi <csardi at rmki.kfki.hu> 14/09/2007 09:27:03 >>> >x[ is.na(x) ] <- 0 > >should work in most cases i think.... only you probably shouldn't be doing that at all. Words like 'bias' spring to mind... Woudn't it be better to accept the NA's and find methods that handle them as genuinely missing. R is usually quite good at that. On Fri, Sep 14, 2007 at 10:08:19AM +0200, Alfredo Alessandrini wrote:> Hi, > > how can I replace NA value with 0:******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}
>>On Fri, Sep 14, 2007 at 09:46:57AM +0100, S Ellison wrote: >> >>... only you probably shouldn't be doing that at all. Words like 'bias' spring to mind... >> >> Woudn't it be better to accept the NA's and find methods that handle them as genuinely missing. >> R is usually quite good at that.And Gabor Csardi <csardi at rmki.kfki.hu> replied>Although in some cases the proper handling of NA values is to treat >them az zeros....Yup. And sometimes not...>I like this list because if you ask a question, >they don't only solve it immediately (in five different ways), but they >persuade you that what you're trying to do is actually >incorrect/stupid/uninteresting or your problem just makes no sense at all. >:)Being a chemist, I have to confess that I can't always tell that what I'm about to attempt is barking, trivial, uninteresting or better done a completely different way; myself, I'd rather be warned too often than left to dig my own pit and fall into it ... On NA's vs zero, I usually have the reverse problem in my corner of the world; folk will often call nondetects 'missing', which is also often a silly thing to do; nondetect means 'I looked and it was too low to see' but NA means 'I didn't look'. All that leaves me a bit nervous about replacing NA with 0 and vice versa ... hence the knee-jerk. Apologies if I'm teaching egg-sucking to an expert. S ******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}
On Fri, Sep 14, 2007 at 10:22:36AM +0100, S Ellison wrote: [...]> knee-jerk. Apologies if I'm teaching egg-sucking to an expert.No apologies please. As i said I _like_ it. Thanks :) G> S > > ******************************************************************* > This email and any attachments are confidential. Any use, ...{{dropped}}
>>> Ted Harding <Ted.Harding at manchester.ac.uk> 14/09/2007 10:59:47 >>> >On the contrary! It adds to our "collective wisdom". > >We all have to suck eggs, and usually can successfully perform the act.Ted, Thanks for the kind remarks. But we'll have to get off the egg topic, or we'll all end up as acknowledged expert suckers.... Steve E ******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}
Hi r-help-bounces at r-project.org napsal dne 17.03.2010 18:01:50:> > Building on the question how to replace NA with 0. > > My data set below has date, station 1, flags for station 1, station 2,flags> for station 2, etc... > > I would like to make the values in the station columns equal to 1 andthe NA> in the station columns equal to 0 and then sum each row for the numberof 1> present in the row. > > head(data.matrix, n=10) > date 05AE005 flg_05AE005 05AF010 flg_05AF010 05BM014flg_05BM014> 1 1900-01-01 NA <NA> NA <NA> NA <NA> > 2 1900-01-02 NA <NA> NA <NA> .23 <NA> > 3 1900-01-03 NA <NA> NA <NA> .45 <NA> > 4 1900-01-04 NA <NA> NA <NA> NA <NA> > 5 1900-01-05 NA <NA> NA <NA> NA <NA> > 6 1900-01-06 NA <NA> NA <NA> NA <NA> > 7 1900-01-07 0.75 <NA> .09 <NA> NA <NA> > 8 1900-01-08 0.87 <NA> .23 <NA> NA <NA> > 9 1900-01-09 0.26 <NA> .78 <NA> NA <NA> > 10 1900-01-10 0.23 <NA> NA <NA> NA <NA> > > # figure out which columns the data are in > colpos <- seq(2, by = 2, length.out = n)What relation is colpos to data.matrix?> > # make value 1 and NA 0 > colpos[!is.na(colpos)] <- 1.0 > colpos[is.na(colpos)] <- 0.0 > > # sum number of values on a given day > rowsum(colpos, )Do you want by chance to get how many nonnumeric values are in each row of your data frame in station columns? If yes, try colpos <- seq(2, 7, by = 2) rowSums(!is.na(data.matrix)[,colpos]) Regards Petr> > > The above script is what i have tried - and does not work. The valuesare> not being replaced with 1s and the NAs with 0s and the rows are notbeing> summed. > > any help would be greatly appreciated. > > E > > -- > View this message in context:http://n4.nabble.com/replace-NA-value-with-0-> tp834446p1596779.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.