Skip to content

if(){} else{}

6 messages · Hans-Juergen Eickelmann, David Scott, Gavin Simpson +2 more

#
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
#
Test should be

if(is.na(mat$Plant)){ ...
On Tue, 5 Dec 2006, Hans-Juergen Eickelmann wrote:

            
_________________________________________________________________
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
#
On Tue, 2006-12-05 at 16:33 +0100, Hans-Juergen Eickelmann wrote:
You only really need rnorm(10), as in ?rnorm, rnorm is defined as
rnorm(n, mean=0, sd=1) and n is the number of observations.
You don't need the ";" at the ends of the lines, and cbind() returns a
matrix, for which you cannot use "$" to access the columns:
[1] "matrix"
NULL

What you are looking for is ifelse(), see ?ifelse, but here is your
example, suitable spaced out and minus the other infelicities.

Plant <- c(NA, 1, 1, 1, NA, NA, NA, NA, NA, 1)
Value1 <- rnorm(10)
Value2 <- rnorm(10)
mat <- data.frame(Plant, Value1, Value2)
mat$Plant1 <- ifelse(is.na(mat$Plant), "A", "B")
[1] "A" "B" "B" "B" "A" "A" "A" "A" "A" "B"
Plant      Value1      Value2 Plant1
1     NA  2.76603270 -0.20435729      A
2      1 -0.54688170 -0.81943566      B
3      1  0.30480812 -0.05404563      B
4      1  1.64959026 -0.10762260      B
5     NA  1.13528236 -0.04670294      A
6     NA  1.55636761  0.87617575      A
7     NA  0.40651924  1.90516887      A
8     NA  1.49827147  0.05080935      A
9     NA -0.04396752  0.53267040      A
10     1  0.42714137 -0.55944595      B

HTH

G
That's not how you use is.na(), see ?is.na, as is.na(x) returns
TRUE/FLASE depending on wither x is NA or not
#
On Wed, 2006-12-06 at 05:26 +1300, David Scott wrote:
No, that won't work because if() is not vectorized:
Warning message:
the condition has length > 1 and only the first element will be used in:
if (is.na(mat$Plant)) {
[1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"

G
#
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:

  
    
#
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)
[1] 0.03 0.02 0.05   NA   NA
[1] 0.28 0.01 0.30   NA   NA
[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 <A.Robinson at ms.unimelb.edu.au>
To:             	Hans-Juergen Eickelmann <EICKELMA at de.ibm.com>
Copies to:      	r-help at stat.math.ethz.ch
Subject:        	Re: [R] if(){} else{}
Petr Pikal
petr.pikal at precheza.cz