Loop over list of integers
for (i in 1:5)
{
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
Iterate over column names to display first observation from every column
for (i in names(mtcars))
{
print(paste("Value for ", i, ": ", mtcars[1, i], sep = ""))
}
## [1] "Value for mpg: 21"
## [1] "Value for cyl: 6"
## [1] "Value for disp: 160"
## [1] "Value for hp: 110"
## [1] "Value for drat: 3.9"
## [1] "Value for wt: 2.62"
## [1] "Value for qsec: 16.46"
## [1] "Value for vs: 0"
## [1] "Value for am: 1"
## [1] "Value for gear: 4"
## [1] "Value for carb: 4"
Specify maximum value from each column in max vector
max = vector("numeric", length(names(mtcars)))
for (i in 1:length(names(mtcars)))
{
max[[i]] = max(mtcars[, i])
}