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
Hi Peter! The 'if' function operate on a single logical expression, while you provided a vector. What your code does at present is subtract 5 from all values in rr.dia.2m if the first value in med.hyper is 1 and otherwise simply returns all values as is. Since you have no reproducible data, it's harder to provide a fix, but you may want to check ?for or (preferably) ?apply. Or simply try : rr.dia.2m.corr<-rr.dia.2m rr.dia.2m.corr[med.hyper==1]<-rr.dia.2m.corr[med.hyper==1]-5 #NOTE: untested hth/ Rafael 2010/6/8 Peter Lercher <Peter.Lercher@i-med.ac.at>> 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 > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
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}}