find the column with lowest value in r - r

I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3)). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row.
How should I do it? It could be duplicated question but I could not find answers.
[,1] [,2] [,3] [,4]
[1,] 10 11 1 12
[2,] 8 9 7 3
[3,] 2 5 6 4

Use max.col:
max.col(-mat)
# [1] 3 4 1

Related

How do drop rows of a matrix, depending on different matrix with the same dimensions in r?

I have to matrices a and b with the same dimension. I would like to drop all the rows in a and b, where a condition in a is not met.
Minimal example:
I would like to only keep rows, where the last element of the row is equal to 4 in a:
a
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
b
[,1] [,2] [,3]
[1,] 7 7 7
[2,] 8 8 8
a[a[,3] == 4,]
[1] 2 3 4
# do stuff, so I am also only left with
b
[1] 8 8 8
What would be a smart way to do this?
If you know the objects are the same dimensions, then logical indices for a will also work for b:
a[a[,3] == 4,]
b[a[,3] == 4,]
If they have different numbers of rows, then this will recycle (with a warning).

R: How to convert a vector into matrix without replicating the vector?

Here's my problem:
I have a vector and I want to convert it into a matrix with fixed number of columns, but I don't want to replicate the vector to fill the matrix when it's necessary.
For example:
My vector has a length of 15, and I want a matrix with 4 columns.I wish to get the matrix wit 15 elements from the vector and a 0 for the last element in the matrix.
How can I do this?
Edit:
Sorry for not stating the question clearly and misguiding you guys with my example. In my program,I don't know the length of my vector, it depends on other parameters and this question involves with a loop, so I need a general solution that can solve many different cases, not just my example.
Thanks for answering.
You could subset your vector to a multiple of the number of columns (so as to include all the elements). This will add necessary amount of NA to the vector. Then convert to matrix.
x = 1:15
matrix(x[1:(4 * ceiling(length(x)/4))], ncol = 4)
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
If you want to replace NA with 0, you can do so using is.na() in another step
We can also do this with dim<- and length<-
n <- 4
n1 <- ceiling(length(x)/n)
`dim<-`(`length<-`(x, n*n1), c(n1, n))
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
data
x <- 1:15

Delete specific values in a matrix according to two position vectors

My aim is to delete specific positions in a matrix according to a vector. Just giving you a small example.
Users_pos <- c(1,2)
Items_pos <- c(3,2)
Given a Matrix A:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
My aim according to the two Vectors User_pos and Item_pos is to delete the following values
A[1,3] and A[3,2]
I'm wondering if there's a possibility to do so without typing in the values for rows and columns by hand.
You can index k elements in a matrix A using A[X], where X is a k-row, 2-column matrix where each row is the (row, col) value of the indicated element. Therefore, you can index your two elements in A with the following indexing matrix:
rbind(Users_pos, Items_pos)
# [,1] [,2]
# Users_pos 1 2
# Items_pos 3 2
Using this indexing, you could choose to extract the information current stored with A[X] or replace those elements with A[X] <- new.values. If you, for instance, wanted to replace these elements with NA, you could do:
A[rbind(Users_pos, Items_pos)] <- NA
A
# [,1] [,2] [,3]
# [1,] 1 NA 3
# [2,] 4 5 6
# [3,] 7 NA 9

matrix row merge in R

I have a matrix
[,1] [,2]
[1,] 2 3
[2,] 3 5
[3,] 7 9
[4,] 11 3
[5,] 11 8
and I want to merge row 1 2 4 5 by their common value.
the result should be output
2 3 5 11 8
Test case:
m <- matrix(c(2,3,7,11,11,3,5,9,3,8),ncol=2)
I'm not sure this is what you want, but it gives the right answer:
unique(c(t(m[c(1,2,4,5),])))
Only two tricky bits here:
need to use c() to collapse the matrix into a single vector
need to use t() to get the matrix collapsed row-wise rather than column-wise to get the ordering as you specified.

R: matrix loop_help me loop through all rows of my matrix. Currently my code is looping through one colomn

I have two matrices I want to perform a loop on. My problem is I am looping for one colomn and do not know how to include the other column, hence my incorrect results. My codes are as below:
t=as.matrix(b)
y=as.matrix(a)
t
a b
[1,] 1 10
[2,] NA 9
[3,] 3 NA
[4,] 4 7
[5,] 5 6
[6,] 3 4
y
c d
[1,] 3 12
[2,] NA 11
[3,] 5 NA
[4,] 6 9
[5,] 7 8
[6,] 3 12
turn
[,1] [,2]
[1,] 0 0
[2,] 0 0
Code:
n=3 #number to consider at a time
runs=2 #total data points divided by 60 to the nearest whole number
turn=matrix(0, nrow=runs,2)
TR = y/t
for (i in 1: runs){
index_start=3*(i-1)+1
index_end= 3*i
turn[i]=mean( TR[index_start:index_end],na.rm=TRUE)
}
turn
[,1] [,2]
[1,] 2.333333 0
[2,] 1.300000 0
The turn output has given correct results for the first column but, as expected, incorrect results for the second column. How do I adjust my loop function? Thank you in advance.
Look at the line
turn[i]=mean( TR[index_start:index_end],na.rm=TRUE)
If you only give a single number in the index of a matrix, it will work out the position by counting down the first column, then down the second, and so on.
So turn[1] refers to the element in the top left corner of the matrix turn, and turn[2] refers to the bottom left corner.
In your loop, i takes the values 1:runs, and runs is 2, so you only assign things to the first two elements of turn.

Resources