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
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.
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.
The %in% operator is used for matching values. “returns a vector of the positions of (first) matches of its first argument in its second”.
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.
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