Skip to content
Prev 94121 / 398500 Next

NA values

On 6/12/2006 8:48 AM, Arun Kumar Saha wrote:
In a vector it is easy, e.g. x[is.na(x)] <- 1000.  A dataframe is a list 
of vectors, so you could iterate through the list, using one of the 
apply functions (or even a for loop):

apply(x, 2, function(col) {col[is.na(col)] <- 1000; col} )

which is essentially a short form for

for (i in 1:ncol(x)) {
   col <- x[,i]
   col[is.na(col)] <- 1000
   x[,i] <- col
}

Duncan Murdoch