Hi Mick,

try the following:

dat[] <- lapply(dat, function(x) ifelse(is.na(x), 0, x))
dat

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
    http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- From: "michael watson (IAH-C)" <[EMAIL PROTECTED]>
To: <r-help@stat.math.ethz.ch>
Sent: Friday, January 14, 2005 12:20 PM
Subject: [R] Replacing NAs in a data frame using is.na() fails if there areno NAs



Hi

This is a difference between the way matrices and data frames work I
guess. I want to replace the NA values in a data frame by 0, and the
code works as long as the data frame in question actually includes an NA
value. If it doesn't, there is an error:


df <- data.frame(c1=c(1,1,1),c2=c(2,2,NA))
df[is.na(df)] <- 0
df

df <- data.frame(c1=c(1,1,1),c2=c(2,2,2))
df[is.na(df)] <- 0
Df

Any help would be appreciated. I could just convert the data frame to a
matrix, execute the code, then convert it back to a data frame, but that
appears long winded.


Thanks
Mick

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to