Hi a little bit quicker solution is based on subsetting and a fact that logical vector can be treated as numeric with FALSE=0 and TRUE = 1
Plant<-sample(c(NA,1), 100000, replace=T); Value1<-rnorm(Plant); Value2<-rnorm(Plant); mat<-data.frame(Plant=Plant,Value1=Value1,Value2=Value2) > system.time(mat$Plant1<-c("B","A")[is.na(mat$Plant)+1]) [1] 0.03 0.02 0.05 NA NA > system.time(mat$Plant2 <- ifelse(is.na(mat$Plant), "A", "B")) [1] 0.28 0.01 0.30 NA NA > with(mat, all.equal(Plant1, Plant2)) [1] TRUE HTH Petr On 6 Dec 2006 at 6:39, Andrew Robinson wrote: Date sent: Wed, 6 Dec 2006 06:39:32 +1100 From: Andrew Robinson <[EMAIL PROTECTED]> To: Hans-Juergen Eickelmann <[EMAIL PROTECTED]> Copies to: r-help@stat.math.ethz.ch Subject: Re: [R] if(){} else{} > Hi Hans, > > try this ... > > mat <- as.data.frame(cbind(Plant,Value1,Value2)) > > mat$Plant1 <- ifelse(is.na(mat$Plant), "A", "B") > > Cheers > > Andrew > > > On Tue, Dec 05, 2006 at 04:33:15PM +0100, Hans-Juergen Eickelmann > wrote: > > Dear R-community, > > my data set looks like 'mat' below. > > > Plant<-c(NA,1,1,1,NA,NA,NA,NA,NA,1); > Value1<-rnorm(1:10); > > Value2<-rnorm(1:10); > mat<-cbind(Plant,Value1,Value2); > I receive > data from two different sites. > One site is identified by an interger > number, the other site has no data in > column Plant=NA. > > My pb: > > > I'm trying to assign labels "A" or "B" to these 2 sites into a new > column, > but my if(){} else{} statement fails with the following > statement: > Error in if (is.na(mat$Plant == TRUE)) { : > > argument is of length zero > > > if(is.na(mat$Plant==TRUE)){mat$Plant1="A"} else{mat$Plant1="B"}; > > I > looked through the avail doc and R-help for some time but wasn't able > to > fix the pb. > > Thx Hans > > > ______________________________________________ > > 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 > and > provide commented, minimal, self-contained, reproducible code. > > -- > Andrew Robinson > Department of Mathematics and Statistics Tel: > +61-3-8344-9763 University of Melbourne, VIC 3010 Australia > Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr > http://blogs.mbs.edu/fishing-in-the-bay/ > > ______________________________________________ > 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 and provide commented, > minimal, self-contained, reproducible code. Petr Pikal [EMAIL PROTECTED] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.