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? Thanks and Regards [[alternative HTML version deleted]]
Hi yes, but I ****do not**** recommend you to do it> df<-data.frame(a=1:10, b=11:20) > df[5,2]<-NA > dfa b 1 1 11 2 2 12 3 3 13 4 4 14 5 5 NA 6 6 16 7 7 17 8 8 18 9 9 19 10 10 20> df[is.na(df)]<-1000 > dfa b 1 1 11 2 2 12 3 3 13 4 4 14 5 5 1000 6 6 16 7 7 17 8 8 18 9 9 19 10 10 20>but in that case you can not simply compute means, sums and other values just by e.g. colSums(df, na.rm=T) HTH Petr On 12 Jun 2006 at 18:18, Arun Kumar Saha wrote: Date sent: Mon, 12 Jun 2006 18:18:33 +0530 From: "Arun Kumar Saha" <arun.kumar.saha at gmail.com> To: "r-help at stat.math.ethz.ch" <R-help at stat.math.ethz.ch> Subject: [R] NA values> 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? > > 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.htmlPetr Pikal petr.pikal at precheza.cz
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
On Mon, 2006-06-12 at 18:18 +0530, 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? > > Thanks and RegardsHi, Arun, sapply(dat, function(x) {x[is.na(x)] <- 1000; x}) e.g.: ## some dummy data dat <- as.data.frame(matrix(rnorm(100), ncol = 10)) ## add some NAs samp <- sample(1:10, 4) dat[samp, -samp] <- NA dat ## replace NAs with 1000 dat <- sapply(dat, function(x) {x[is.na(x)] <- 1000; x}) dat HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% * Note new Address, Telephone & Fax numbers from 6th April 2006 * %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson ECRC & ENSIS [t] +44 (0)20 7679 0522 UCL Department of Geography [f] +44 (0)20 7679 0565 Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street [w] http://www.ucl.ac.uk/~ucfagls/cv/ London, UK. [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%