change "mean <- mean(data)" to "mean <- colMeans(data)".