[R] problem with if else statement

Martyn Byng Martyn.Byng at nag.co.uk
Tue Jun 8 11:46:17 CEST 2010


Hi,

You are using if/then/else which is a logical control statement and so doesn't return a value, see 
?if
for details.

You are probably looking for the ifelse function.

Martyn

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Lercher
Sent: 08 June 2010 10:18
To: r-help at r-project.org
Subject: [R] problem with if else statement

Dear colleagues,

What did I not understand ?

->my intention
I want to create a new variable:
In plain language:
If someone is taking anithypertensive treatment (med.hyper==1)
table(med.hyper)
med.hyper
  0   1
472  97
I want to subtract 5 mmHg (rr.dia.2m-5) from the measured diastolic blood pressure (rr.dia.2m) if not treated - the value of the measured diastolic blood pressure should remain the same

->my code (data frame is attached !)
rr.dia2.corr<-if(med.hyper==1)
{
rr.dia.2m-5
} else
{
rr.dia2.corr==rr.dia.2m
}

R warning
Warnmeldung:
In if (med.hyper == 1) { :
  Bedingung hat Länge > 1 und nur das erste Element wird benutzt "Only first condition is use"
which you can see from the result (simply 5 mm was subtracted and the second condition was taken into account)

 > describe(rr.dia.2m)
rr.dia.2m
      n missing  unique    Mean     .05     .10     .25     .50     
.75     .90     .95
    546      26     110   85.32   67.00   71.50   77.62   84.50   
92.00   99.25  105.00

lowest :  45.0  57.0  60.0  61.0  62.0, highest: 127.5 128.5 129.5 142.5
146.5
 > describe(rr.dia2.corr)
rr.dia2.corr
      n missing  unique    Mean     .05     .10     .25     .50     
.75     .90     .95
    546      26     110   80.32   62.00   66.50   72.62   79.50   
87.00   94.25  100.00

lowest :  40.0  52.0  55.0  56.0  57.0, highest: 122.5 123.5 124.5 137.5
141.5
--------------------
OS windows
R version 2.10.1 (2009-12-14)
attached R modules: design+hmisc
------------------
Thank you so much

Peter



________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}



More information about the R-help mailing list