Question

Basically I'm looking for an equivalent of

for (i in 1:nrow(mydata)) {
    if(mydata$alive[i]) { mydata$result[i] = mydata$alive_value; }
    else { mydata$result[i] = mydata$dead_value; }
}

That would be along the lines of

mydata$result <- func_if(mydata$alive,mydata$alive_value,mydata$dead_value)

Does something like that exist?

Was it helpful?

Solution

You're looking for ifelse. Documentation: http://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html.

mydata$result <- ifelse(mydata$alive, mydata$alive_value, mydata$dead_value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top