[R] NA values

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jun 12 15:20:50 CEST 2006


On 6/12/2006 8:48 AM, Arun Kumar Saha wrote:
> Dear all R users,
> 
> I am wondering whether there is any way to replace all "NA" values in a data
> frame by some numerical value, suppose 1000?

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

> 
> Thanks and Regards
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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



More information about the R-help mailing list