Hello i have a 3 x 2 with the age and the weight.
I have to find the maximum of the age and the weight with a loop.
can somebody help me?
Hello i have a 3 x 2 with the age and the weight.
I have to find the maximum of the age and the weight with a loop.
can somebody help me?
Doing this with a loop in R
is not how one would usually approach this but here is a way to achieve the colmax using a for loop :
set.seed(1)
# matrix
m <- matrix(rnorm(6), nrow = 3)
# storage for colmax
l <- NULL
# for loop across columns
for( i in 1:ncol(m)){
l[i] <- max(m[,i])
}
# colmax
l
[1] 0.1836433 1.5952808
You don't need a loop R has vectorised functions
sample_matrix <- matrix(c(30,22,40,70,80,50),nrow = 3)
apply(sample_matrix,MARGIN = 2,FUN = max)
#> [1] 40 80
Created on 2020-01-05 by the reprex package (v0.3.0)