# Problems picking out minimum values of a column of a matrix

I am a beginer and i have problems picking out minimum values ​​of a column of a matrix..

a<-c(1,4,6,4,1)
b<-c(4,16,24,16,4)
c<-c(6,24,36,24,6)
d<-c(4,16,24,16,4)
e<-c(1,4,6,4,1)
cbind(a,b,c,d,e)
gauss.filter<-cbind(a,b,c,d,e)
gauss_std<-gauss.filter/sum(gauss.filter)
min(gauss.filter)
min(gauss.filter[1,2,3,4])
min(gauss.filter(c(1,2,3,4)))
dplyr::filter(min(gauss.filter))
dplyr::filter(gauss.filter,min(iris))
dplyr::filter(gauss.filter,min(c(1,2,3,4,5))
dplyr::filter(gauss.filter,min(1-5))
dplyr::filter(row(gauss.filter)
min(row(gauss.filter))
min(row.names(gauss.filter))
min(row(1-5))
dplyr::filter(gauss.filter(min(row())))
gauss.filter(min(iris))

Hi there, if I understand you correctly, you want to return the minimum value for each column of your `gauss.filter` matrix?

If so, I would suggest using the `apply()` function. As the name suggests, this function applies a function over a margin of a matrix like object. It would look something like this:

``````a <- c(1, 4, 6, 4, 1)
b <- c(4, 16, 24, 16, 4)
c <- c(6, 24, 36, 24, 6)
d <- c(4, 16, 24, 16, 4)
e <- c(1, 4, 6, 4, 1)

gauss.filter <- cbind(a, b, c, d, e)

apply(
X = gauss.filter,
MARGIN = 2,
FUN = min
)
#> a b c d e
#> 1 4 6 4 1
``````

The `apply` function above can be read as, "return the minimum value of each column of the `gauss.filter` matrix." (If `MARGIN` had been set to 1, it would have returned the minum value for each row instead of each column.

As an aside, I would strongly suggest taking a look at this post which has a few solid resources for learning R. Good luck!

1 Like

To do this with tidyverse functions, you could do the following:

``````library(tidyverse)

gauss.filter <- cbind(a, b, c, d, e)

gauss.filter %>%
as.data.frame() %>%
summarise_all(min)
``````
``````  a b c d e
1 1 4 6 4 1
``````

`gauss.filter` is a matrix, but dplyr functions expect a data frame, so we first convert it to a data frame. Then `summarise_all` runs the `min` function on every column of `gauss.filter`.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.