Kars
Kars

Reputation: 23

Correct use of if-else function in R

I'm doing a research project on the volume of calcification on the symptomatic or asymptomatic side of stroke.

The symptomatic side can be 1 (right) or 2 (left). For all cases, I calculated the volume of calcification on the right and left side separetly. Next, I want to compare both sides (symp. vs asymp) with respect to the total volume of calcification.

I thougt the following code was correct:

ds$totalcalcSymp <- if(ds$sympt_side==2) ds$totalcalcL else 
  (ds$totalcalcR)

ds$totalcalcAsymp <- if(ds$sympt_side>1) ds$totalcalcR else
  (ds$totalcalcL)

However, the above R code seems not to work. For the symptomatic side I get values that are similar to totalcalcR (right side). And for the asymptomatic side values of totalcalcL (left side).

I also tried work with only if statements, but still I get the same results as above. Is the problem related to the warning messages I also receive?

the condition has length > 1 and only the first element will be used

What is an appropriate solution to link the correct side (totalcalcR or totalcL) to the symptomatic and asymptomic side of stroke?

Thank you all in advance!

Upvotes: 2

Views: 78

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Try using ifelse():

ds$totalcalcSymp <- ifelse(ds$sympt_side==2, ds$totalcalcL, ds$totalcalcR)

Upvotes: 1

Related Questions