Re: [R] problem with if else statement

From: Martyn Byng <Martyn.Byng_at_nag.co.uk>
Date: Tue, 08 Jun 2010 10:46:17 +0100

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}}

R-help_at_r-project.org 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. Received on Tue 08 Jun 2010 - 09:52:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Jun 2010 - 11:20:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive