Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match with language R for getting the position

Tags:

r

match

I am using match for getting if an element is in a list. For example my list is:

  c("a","b","h","e"...) and so on

if I want to see if element h is in the list I am using match in this way:

  if ("h" %in% v){do something}

How I can get the position of where it finds the element in the list? Thanks

like image 492
Layla Avatar asked Oct 08 '12 16:10

Layla


People also ask

What does match () do in R?

match() function in R Language is used to return the positions of the first match of the elements of the first vector in the second vector. If the element is not found, it returns NA.

Which of the following operators in R can be used for the value matching?

match: Value Matching arules defines additional binary operators for matching itemsets: %pin% uses partial matching on the table; %ain% itemsets have to match/include all items in the table; %oin% itemsets can only match/include the items in the table.

Which of the following operators in R can be used for the value matching select one check in out match?

The %in% operator is used for matching values. “returns a vector of the positions of (first) matches of its first argument in its second”.


1 Answers

If you want to know the position use which

l <- c("a","b","h","e")
which(l=='h') 
[1] 3   # It'll give you the position, 'h' is the third element of 'l'

Note that l is a vector, not a list as you mentioned.

like image 165
Jilber Urbina Avatar answered Oct 18 '22 14:10

Jilber Urbina