Our indexing set can be elements aside from numbers.
id <- c("Total.Points", "Minutes", "Games")
# Loops through id and prints out each string
for (colname in id) {
print(colname)
}
## [1] "Total.Points"
## [1] "Minutes"
## [1] "Games"
for(colname in id) {
print(paste(colname, mymean(nba[, colname])))
# paste() is used to print variables and strings together
}
## [1] "Total.Points 2037.2899408284"
## [1] "Minutes 4783.63313609467"
## [1] "Games 200.615384615385"
In the second for loop, we are cycling though our indexset and printing the column name along with its associated average.