Use the rowMeans()
function:
final$mean <- rowMeans(final, na.rm=TRUE)
Note that you should avoid using loops for your everyday R
operations. If you want to iterate over all rows yourself, you can use the apply()
function like this:
final$mean <- apply(final, 1, function(x) { mean(x, na.rm=TRUE) })
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…