Peng Kate
Peng Kate

Reputation: 1

Using for loop in R: compute the column means

I tried to do the exercise "compute the mean of every column in mtcars". I know the easiest way is to use colmeans()

colMeans(mtcars)

but I still want to figure out the way by using for loop. Here is my code, but not working. I have tried many times, but cant find out the error (quite frustrating...). Your reply will be deeply appreciated. Thank you.

for (i in c(1:11)) {   #mtcars has 11 columns
    y1<-mean(mtcars[,i])
    y2<-c(y1,y2)
}
y2

Kate


Thank you so much for replies. Following the online comments, I updated the code as follows:

y2<-numeric()
 for (i in seq_along(mtcars)) {
 y1<-mean(mtcars[,i])
 y2<-c(y1,y2)
}
y2
 [1]   2.812500   3.687500   0.406250   0.437500  17.848750   3.217250
 [7]   3.596563 146.687500 230.721875   6.187500  20.090625

If using colMeans()...

colMeans(mtcars)
    mpg        cyl       disp         hp       drat         wt       qsec 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
    vs         am       gear       carb 
 0.437500   0.406250   3.687500   2.812500 

It is quite interesting to see the list is totally reverse (compared to the code in the first part). Then I found out the issue in y2<-c(y1,y2) if I change original y2<-c(y1,y2) into...

y2<-c(y2,y1) 

The final version....

y2<-numeric()
 for (i in seq_along(mtcars)) {
 y1<-mean(mtcars[,i])
 y2<-c(y2,y1)
}
y2
[1]  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250
[7]  17.848750   0.437500   0.406250   3.687500   2.812500

This result finally matched with the results in colMeans()!!

Thank you all once again for the help!!

Kate

Upvotes: 0

Views: 1700

Answers (1)

s_baldur
s_baldur

Reputation: 33488

Here is a standard way to do it with a loop:

# Extract the number of columns
ncols <- length(mtcars)
# Initialize your mean vector (this will make the loop run much faster)
column_means <- vector(mode = "numeric", length = ncols)
# Now loop through each column
for (i in 1:ncols) {   
  column_means[i] <- mean(mtcars[[i]])
}
# Why not turn our mean vector into a named vector so we can better make sense 
# of the numbers
names(column_means) <- names(mtcars)
column_means

       mpg        cyl       disp         hp       drat         wt       qsec         vs         am       gear 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500   0.406250   3.687500 
      carb 
  2.812500 

But how to make the original code work?

y2 <- NULL
for (i in c(1:11)) {   #mtcars has 11 columns
    y1<-mean(mtcars[,i])
    y2<-c(y2, y1)
}

Upvotes: 2

Related Questions