I am fairly new to R and have tried several versions of the following statement with no avail. What I need is to create a new variable MAXVO2 and perform calculations adjusted for Age and Sex and then round it to 2 decimals.
The main error is If and Else. R is case sensitive, and it supports if and else only.
Also, the expected format is the following, you can't start else in a new line.
if (condition1) {
expr1
} else if (condition2) {
expr2
} else if (condition3) {
expr3
} else {
expr4
}
I think ed.frame is a data frame for you, and usually that has more than one rows. If that is the case, the result is not going to be what you expect. This is because if checks using first element only. You'd need a for loop to check for each row.
As Richard suggests above, there are alternative ways to do what you want, with or without external libraries. Those will certainly be better than a combination of for loop and if else. But this is such a basic thing in R that in my opinion, you should know these things well first before going on to those.
Thank you. I will certainly look into your suggestions. About last brace lacks opening brace, I though I had in this statement: Else **{**ed.frame$MAXVO2 <- 999
}
That's a great point. Yes, ed.frame is my data frame and has many rows. No wonder when I tried using mutate, the error was something like the code applies to only 1 record but data set has 1500+ rows. I definitely look into Richard's suggesiotn.