static_rtti
static_rtti

Reputation: 56282

Is there a "functional if" in R?

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?

Upvotes: 0

Views: 107

Answers (1)

thompsor
thompsor

Reputation: 147

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)

Upvotes: 5

Related Questions