I’m having a simple issue that i couldn’t solve with the for loop.
I calculate and save the mean of a column each time inside my for loop.
In the first loop, the mean is saved to the variable. but in the second loop the mean of column2 will replace the mean of column1. This is a very basic question but I have no idea how to do that.
#read.table(file1)
for (x in 1:100){
f <- mean(file1[,x])
}
I want to save the mean values for all columns in 1 variable (assume it is called “f”).
f <- c(meancol1, meancol2, meancol3... meancol100)
Any ideas?
Even simpler you could use
colMeansfunction. Here’s an exampleTake a look at
?colMeansIf you have non-numeric columns you can skip them autotically using
sapplyinsidecolMeansfor example: