Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, Substitute NAs by zero

Tags:

r

zoo

How can I substitute NA values by zero in a R zoo series? I've been reading about na.locf and na.omit but I think none of them do what I need.

thanks.

like image 895
skan Avatar asked Dec 05 '22 01:12

skan


2 Answers

I'm not familiar with the Zoo Series but this is how you do it with vectors. This Should work.

x = c(NA,1,2,3)
x[ is.na(x) ] <- 0 
print (x)
like image 198
st0le Avatar answered Dec 21 '22 10:12

st0le


Use na.fill. If z is the zoo series:

na.fill(z, fill = 0)
like image 32
G. Grothendieck Avatar answered Dec 21 '22 08:12

G. Grothendieck