[R] How to replace NA with zero (0)

Mercier Eloi emercier at chibi.ubc.ca
Fri May 4 03:37:14 CEST 2012


> Hello,
>   When i generate data with the code below there appear NA as part of the generated data, i prefer to have zero (0) instead of NA on my data.
> Is there a command i can issue to replace the NA with zero (0) even if it is after generating the data? 
> Thank you
?Surv

time2: ending time of the interval for interval censored or counting
           process data only

and

w=Surv(r,t,type="interval2")
Warning message:
In Surv(r, t, type = "interval2") :
   Invalid interval: start>  stop, NA created

=>  r needs to be greater that t.

> Just ***DON'T***.  NAs and zeroes are completely different concepts. 
> You will simply confuse everybody including yourself.     cheers, 
>         Rolf Turner 

I can only agree with that.


On 12-05-03 05:18 PM, Steve Lianoglou wrote:

> Also,
>
> On Thu, May 3, 2012 at 1:57 PM, J Toll<jctoll at gmail.com>  wrote:
>> On Thu, May 3, 2012 at 10:43 AM, Christopher Kelvin
>> <chris_kelvin2001 at yahoo.com>  wrote:
>>
>>> Is there a command i can issue to replace the NA with zero (0) even if it is after generating the data?
>> Chris,
>>
>> I didn't try your example code, so this suggestion is far more
>> general, but you might try something along the lines of:
>>
>> x[which(is.na(x))]<- 0
> Random note from left field: the call to `which` is unnecessary here.
>
> -steve
>
set.seed(1)
r1=runif(10)
set.seed(2)
t=runif(10)
w=Surv(r,t,type="interval2")

w1=w
w1[is.na(w)] = 0
w1
  [1] 0.00000000+             [0.37212390, 0.7023740] [0.57285336, 
0.5733263]
  [4] 0.00000000+             [0.20168193, 0.9438393] [0.89838968, 
0.9434750]
  [7] 0.00000000+             [0.66079779, 0.8334488] 0.00000000+
[10] [0.06178627, 0.5499837]

w2=w
w2[which(is.na(w2))] = 0
w2
  [1] [0.00000000, 0.1848823] [0.37212390, 0.7023740] [0.57285336, 
0.5733263]
  [4] [0.00000000, 0.1680519] [0.20168193, 0.9438393] [0.89838968, 
0.9434750]
  [7] [0.00000000, 0.1291590] [0.66079779, 0.8334488] [0.00000000, 
0.4680185]
[10] [0.06178627, 0.5499837]

Eloi

-- 
Eloi Mercier
Bioinformatics PhD Student, UBC
Paul Pavlidis Lab
2185 East Mall
University of British Columbia
Vancouver BC V6T1Z4



More information about the R-help mailing list