I want to use pmax
to compute the row-wise maximium of a matrix A
:
A = matrix(sample(1:20),10,2)
pmax(A[,1],A[,2])
this works fine. But the problem is that I don't know the size of A, so the call to pmax
should be able to split the matrix by columns and supply each column as an argument. how to do that? For example, I may in the next instance have
A = matrix(sample(1:20),5,4)
But I don't want to have to rewrite by hand every time to
pmax(A[,1],A[,2],A[,3],A[,4])
in fact, I can't because the size of A
is unknown before the start of the program.
A matrix with the same number of rows and columns is called a square matrix.
For adding a column to a Matrix in we use cbind() function. To know more about cbind() function simply type ? cbind() or help(cbind) in R.
A matrix is denoted by [aij]mxn, where i and j represent the position of elements in the matrix, row-wise and column-wise, m is the number of rows and n is the number of columns.
You can use do.call
:
do.call(pmax, as.data.frame(A))
Just use apply
with max
instead...
apply( A , 1 , max )
# [1] 6 11 20 18 17
pmax(A[,1],A[,2],A[,3],A[,4])
# [1] 6 11 20 18 17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With