For this particular dataframe I want to get the mean and sd of all the numeric columns. So I am using the following block of code:
for (col in colnames(cereal.data)) {
if (is.numeric(cereal.data$col)) {
mean(cereal.data$col)
}
}
But this does not seem to work so I tried the it like
columns <- colnames(cereal.data)
for (col in 1:length(columns)) {
if (is.numeric(cereal.data[[col]])) {
mean(cereal.data[[col]])
}
}
How can I make this work?
I am expecting to have the mean and sd of each numeric columns of the dataframe and tried using for loop to iterate over the column names and checking is they are numeric.