Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify Columns Index Matching Given Vector of String

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?

like image 987
user1234440 Avatar asked Feb 05 '13 03:02

user1234440


People also ask

How do you find the index of an element in a list in R?

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.

How do you find the index of an array in R?

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.


1 Answers

try

 which(colnames(sample_matrix) %in% x)
like image 170
Aditya Sihag Avatar answered Sep 21 '22 01:09

Aditya Sihag