? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070328/95a9e643/attachment.pl
On Wed, 2007-03-28 at 16:21 +0200, Sergio Della Franca wrote:> Dearl R-Helpers, > > > I have the following data set: > > YEAR PRODUCTS > 1990 2478 > 1995 3192 > 2000 NA > 2005 1594 > > > I wanto to replace NA values, in the PRODUCTS column, with 0. > > > How can i obtain this? > > > Thak you in advance. > > > Sergio Della FrancaSeveral ways: 1. Using replace(): DF$PRODUCTS <- replace(DF$PRODUCTS, is.na(DF$PRODUCTS), 0) 2. Using regular indexing: DF$PRODUCTS[is.na(DF$PRODUCTS)] <- 0 See ?replace and ?is.na That being said, be very cautious about doing this. Most R functions are designed to handle NA values in very predictable ways, but not so with 0 values. See ?NA for more information. For example:> DFYEAR PRODUCTS 1 1990 2478 2 1995 3192 3 2000 NA 4 2005 1594> mean(DF$PRODUCTS)[1] NA> mean(DF$PRODUCTS, na.rm = TRUE)[1] 2421.333 Now with:> DFYEAR PRODUCTS 1 1990 2478 2 1995 3192 3 2000 0 4 2005 1594> mean(DF$PRODUCTS)[1] 1816 HTH, Marc Schwartz
See ?is.na and use its result for indexing. Uwe Ligges Sergio Della Franca wrote:> Dearl R-Helpers, > > > I have the following data set: > > YEAR PRODUCTS > 1990 2478 > 1995 3192 > 2000 NA > 2005 1594 > > > I wanto to replace NA values, in the PRODUCTS column, with 0. > > > How can i obtain this? > > > Thak you in advance. > > > Sergio Della Franca > > [[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 > and provide commented, minimal, self-contained, reproducible code.
This could work, but not with big matrix! year <- c(1990,1995,2000,2005) Prod <- c(2478,3192,NA,1594) matrix <- data.frame(cbind(year,Prod)) for (i in 1:dim(matrix)[1]) { if (is.na(matrix[i,2])) {matrix[i,2] <- 0} }
? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070330/65a99309/attachment.pl
Sergio Della Franca wrote:> Dear R-Helpers, > > > I have the following data set(y): > > Test_Result #_Test > t 10 > f 14 > f 25 > f NA > f 40 > t 45 > t 44 > <NA> 47 > t NA > > > I want to replace the NA values with the following method: > - for the numeric variable, replace NA with median > - for character variable , replace NA with the most frequent level > > If i use x<-na.roughfix(y) the NA values are correctly replaced. > But if i x<-na.roughfix(y$Test_Result) i obtain the following error: > > roughfix can only deal with numeric data. > > How can i solve this proble that i met every time i want to replace only the > NA values of a column (type character)? >Hi Sergio, In the prettyR package is the Mode function. This returns the mode of a vector as a character string. So I think this would do what you want: library(prettyR) testvec<-c(sample(LETTERS[1:4],20,TRUE),NA,NA) testvec[is.na(testvec)]<-Mode(testvec) You could do the same trick with the median function for the numeric values. Jim