Re: [R] replace NA value with 0

2010-03-18 Thread Petr PIKAL
Hi

r-help-boun...@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 and 
the NA
 in the station columns equal to 0 and then sum each row for the number 
of 1
 present in the row.
 
 head(data.matrix, n=10)
  date 05AE005 flg_05AE005 05AF010 flg_05AF010 05BM014 
flg_05BM014
 1  1900-01-01  NANA  NANA  NA NA
 2  1900-01-02  NANA  NANA  .23 NA
 3  1900-01-03  NANA  NANA  .45 NA
 4  1900-01-04  NANA  NANA  NA NA
 5  1900-01-05  NANA  NANA  NA NA
 6  1900-01-06  NANA  NANA  NA NA
 7  1900-01-07  0.75  NA  .09NA  NA NA
 8  1900-01-08  0.87  NA  .23NA  NA NA
 9  1900-01-09  0.26  NA  .78NA  NA NA
 10 1900-01-10  0.23  NA  NANA  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 values 
are
 not being replaced with 1s and the NAs with 0s and the rows are not 
being
 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@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.

__
R-help@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.


Re: [R] replace NA value with 0

2010-03-17 Thread ehux

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 and the NA
in the station columns equal to 0 and then sum each row for the number of 1
present in the row.

head(data.matrix, n=10)
 date 05AE005 flg_05AE005 05AF010 flg_05AF010 05BM014 flg_05BM014
1  1900-01-01  NANA  NANA  NANA
2  1900-01-02  NANA  NANA  .23NA
3  1900-01-03  NANA  NANA  .45NA
4  1900-01-04  NANA  NANA  NANA
5  1900-01-05  NANA  NANA  NANA
6  1900-01-06  NANA  NANA  NANA
7  1900-01-07  0.75  NA  .09NA  NANA
8  1900-01-08  0.87  NA  .23NA  NANA
9  1900-01-09  0.26  NA  .78NA  NANA
10 1900-01-10  0.23  NA  NANA  NANA

# figure out which columns the data are in
colpos - seq(2, by = 2, length.out = n)

# 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, )


The above script is what i have tried - and does not work. The values are
not being replaced with 1s and the NAs with 0s and the rows are not being
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@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.


[R] replace NA value with 0

2007-09-14 Thread Alfredo Alessandrini
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@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.


Re: [R] replace NA value with 0

2007-09-14 Thread Gabor Csardi
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@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 [EMAIL PROTECTED]MTA RMKI, ELTE TTK

__
R-help@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.


Re: [R] replace NA value with 0

2007-09-14 Thread Gabor Csardi
On Fri, Sep 14, 2007 at 09:46:57AM +0100, S Ellison wrote:
 
 
  Gabor Csardi [EMAIL PROTECTED] 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.

Although in some cases the proper handling of NA values is to treat 
them az zeros 

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.
:)

Gabor

 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}}
 
 __
 R-help@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 [EMAIL PROTECTED]MTA RMKI, ELTE TTK

__
R-help@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.


Re: [R] replace NA value with 0

2007-09-14 Thread S Ellison


 Ted Harding [EMAIL PROTECTED] 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}}

__
R-help@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.


Re: [R] replace NA value with 0

2007-09-14 Thread Ted Harding
On 14-Sep-07 09:22:36, S Ellison wrote:
 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

On the contrary! It adds to our collective wisdom.

We all have to suck eggs, and usually can successfully perform the act.
But it is useful to learn about people's experiences of when it is wise
to spit out the result.

Thanks, and best wishes,
Ted.


E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 14-Sep-07   Time: 10:59:42
-- XFMail --

__
R-help@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.


Re: [R] replace NA value with 0

2007-09-14 Thread John Kane
Seconded.
--- Greg Snow [EMAIL PROTECTED] wrote:

 
  
 
  -Original Message-
  From: [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED] On Behalf Of
 Gabor Csardi
  Sent: Friday, September 14, 2007 2:56 AM
  To: S Ellison
  Cc: [EMAIL PROTECTED]
  Subject: Re: [R] replace NA value with 0
 
 I nominate the following paragraph for the fortunes
 package
 
  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.
  :)
  
  Gabor
 
 __
 R-help@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.


__
R-help@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.