[R] problem with if else statement

Petr PIKAL petr.pikal at precheza.cz
Tue Jun 8 13:01:52 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 08.06.2010 11:46:17:

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

Or use possibility of easy conversion logical (T/F) to numeric (1/0)

rr.dia.2m - (med.hyper==1)*5

Regards
Petr

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



More information about the R-help mailing list