Re: [R] Changing zeros to NAs in a data frame
Laura Holt wrote: Dear R People: I have a data frame with some columns that are numeric and some which are factors. There are zeros in the numeric columns and I would like to change them to NAs. However, there are zeros in some of the factor columns, and I would like them to be left alone. Is there a global way to do this, please? I was thinking about use the results from str but am not sure. R Version 2.0.0 Windows. Let x be your data.frame. Then use: is.num - sapply(x, is.numeric) x[is.num] - lapply(x[is.num], function(y) ifelse(y == 0, NA, y)) HTH, --sundar __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Changing zeros to NAs in a data frame
This isn't pretty but it's a way to do it: foo - data.frame(x = c(1,0,1,1,0,2,4), y = as.factor(c(0,2,1,1,0,3,1))) Zero2NA - function(x){ if(is.numeric(x)) { x[x == 0] - NA; } return(x) } foo2 - as.data.frame(lapply(foo, Zero2NA)) foo foo2 HTH, Andy -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Laura Holt Sent: Tuesday, November 16, 2004 11:31 AM To: [EMAIL PROTECTED] Subject: [R] Changing zeros to NAs in a data frame Dear R People: I have a data frame with some columns that are numeric and some which are factors. There are zeros in the numeric columns and I would like to change them to NAs. However, there are zeros in some of the factor columns, and I would like them to be left alone. Is there a global way to do this, please? I was thinking about use the results from str but am not sure. R Version 2.0.0 Windows. Thanks in advance. Sincerely, Laura Holt mailto: [EMAIL PROTECTED] Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Changing zeros to NAs in a data frame
On Tue, 16 Nov 2004, Laura Holt wrote: Dear R People: I have a data frame with some columns that are numeric and some which are factors. There are zeros in the numeric columns and I would like to change them to NAs. However, there are zeros in some of the factor columns, and I would like them to be left alone. Is there a global way to do this, please? I was thinking about use the results from str but am not sure. myDF - data.frame(a=0:4, b=letters[1:5], c=-2:2) myDF[] - lapply(myDF, function(x) if(is.numeric(x)) {x[x==0] - NA; x} else x) myDF a b c 1 NA a -2 2 1 b -1 3 2 c NA 4 3 d 1 5 4 e 2 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html