문제

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?

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top