I've got a vector of string
x<-c('a','b')
and I have an matrix with multiple columnsl; which contains names in that vector of string. I would like to get the column numbers/index which matches their names.
which(colnames(sample_matrix) == x)
This above works when x is not a vector but a single element. Any solutions?
match() function basically returns the vector of indexes that satisfies the argument given in the match() function. Example 1: In our case, we first create the vector of values (0,1,2,3,4,5,6,7,8,9), and then we try to get the index value of the element “5” with the help of the match() function.
Get Indices of Specified Values of an Array in R Programming – arrayInd() Function. arrayInd() function in R Language is used to get the indices of the values passed to the function as argument. This function takes values and the array in which the values are to be searched and returns the indices for each match found.
try
which(colnames(sample_matrix) %in% x)
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