# Sapply is not working like for loop

sapply(c(2:5), function(y){
x[y,]<-x[y,]-(x[1,]*x[y, 1]/x[1, 1])})

for (i in c(2:5)){
x[i,]<-x[i,]-(x[1,]*x[i, 1]/x[1,1])}


Can someone explain to me why I am getting different results, I am coming from Python, I am not used to these apply functions of R

Thanks!

• I get the same result from both pieces of code. Both give error messages. You need to tell us what x is. – G5W Sep 21 '19 at 22:10
• X is a 5x5 matrix; I realized later that Sapply takes the rows and puts them as columns that is why I was getting different results. Thanks for your reply! – Guneykan Ozkaya Sep 22 '19 at 14:27