[R] if(){} else{}

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Dec 5 17:48:47 CET 2006


On Wed, 2006-12-06 at 05:26 +1300, David Scott wrote:
> Test should be
> 
> if(is.na(mat$Plant)){ ...

No, that won't work because if() is not vectorized:

> if(is.na(mat$Plant)){mat$Plant1 <- "A"} else{mat$Plant1 <- "B"}
Warning message:
the condition has length > 1 and only the first element will be used in:
if (is.na(mat$Plant)) {
> mat$Plant1
 [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"

G

> 
> 
> 
> On Tue, 5 Dec 2006, 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 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.
> >
> 
> _________________________________________________________________
> David Scott	Visiting (Until January 07)
>  		Department of Probability and Statistics
>  		The University of Sheffield
>  		The Hicks Building
>  		Hounsfield Road
>  		Sheffield S3 7RH
>  		United Kingdom
> Phone:	+44 114 222 3908
> Email:	d.scott at auckland.ac.nz
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC & ENSIS, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list