Hi, I have newbie question. Suppose I have the following data: temp <- data.frame(type1 = c("male", "female", "male", "female", "female"), type2 = c("low", "med", "high", "low", "med"), a = c(1,2,4, NA, 3), b .... [TRUNCATED] temp type1 type2 a b c 1 male low 1 5 0 2 female med 2 NA 0 3 male high 4 5 1 4 female low NA 1 1 5 female med 3 2 NA how to change all NA into 0 (zero) ? so I would have the following: temp type1 type2 a b c 1 male low 1 5 0 2 female med 2 0 0 3 male high 4 5 1 4 female low 0 1 1 5 female med 3 2 0 I've been trying with which() but without success. Any pointer is appreciated. Thank you, Ferry [[alternative HTML version deleted]]
Sorry, the data is: temp <- data.frame(type1 = c("male", "female", "male", "female", "female"), type2 = c("low", "med", "high", "low", "med"), a = c(1,2,4, NA, 3), b c(5,NA,5,1,2), c = c(0,0,1,1,NA)) On Tue, Mar 17, 2009 at 4:28 PM, Ferry <fmi.mlist@gmail.com> wrote:> Hi, > > I have newbie question. Suppose I have the following data: > > temp <- data.frame(type1 = c("male", "female", "male", "female", "female"), > type2 = c("low", "med", "high", "low", "med"), a = c(1,2,4, NA, 3), b > .... [TRUNCATED] > > temp > type1 type2 a b c > 1 male low 1 5 0 > 2 female med 2 NA 0 > 3 male high 4 5 1 > 4 female low NA 1 1 > 5 female med 3 2 NA > > how to change all NA into 0 (zero) ? so I would have the following: > > temp > type1 type2 a b c > 1 male low 1 5 0 > 2 female med 2 0 0 > 3 male high 4 5 1 > 4 female low 0 1 1 > 5 female med 3 2 0 > > I've been trying with which() but without success. > > Any pointer is appreciated. > > Thank you, > > Ferry >[[alternative HTML version deleted]]
> temptype1 type2 a b c 1 male low 1 5 0 2 female med 2 NA 0 3 male high 4 5 1 4 female low NA 1 1 5 female med 3 2 NA> temp[is.na(temp)] <- 0 ## the magic words...> temptype1 type2 a b c 1 male low 1 5 0 2 female med 2 0 0 3 male high 4 5 1 4 female low 0 1 1 5 female med 3 2 0>You are in a spot of bother, though, if your factors have missing values also. Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ferry Sent: Wednesday, 18 March 2009 9:29 AM To: r-help at r-project.org Subject: [R] converting null to some values Hi, I have newbie question. Suppose I have the following data: temp <- data.frame(type1 = c("male", "female", "male", "female", "female"), type2 = c("low", "med", "high", "low", "med"), a = c(1,2,4, NA, 3), b .... [TRUNCATED] temp type1 type2 a b c 1 male low 1 5 0 2 female med 2 NA 0 3 male high 4 5 1 4 female low NA 1 1 5 female med 3 2 NA how to change all NA into 0 (zero) ? so I would have the following: temp type1 type2 a b c 1 male low 1 5 0 2 female med 2 0 0 3 male high 4 5 1 4 female low 0 1 1 5 female med 3 2 0 I've been trying with which() but without success. Any pointer is appreciated. Thank you, Ferry [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.