Re: [R] NA values

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Mon 12 Jun 2006 - 23:20:50 EST

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@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 Received on Tue Jun 13 00:47:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 13 Jun 2006 - 02:12:07 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.