r2 calculation in r code example Example: how to get the r2 value in r rsq <- function (x, y) cor(x, y) ^ 2