﻿ Is There an R Function For Finding the Index of an Element in a Vector - ITCodar

# Is There an R Function For Finding the Index of an Element in a Vector

## Is there an R function for finding the index of an element in a vector?

The function `match` works on vectors:

``x <- sample(1:10)x# [1]  4  5  9  3  8  1  6 10  7  2match(c(4,8),x)# [1] 1 5``

`match` only returns the first encounter of a match, as you requested. It returns the position in the second argument of the values in the first argument.

For multiple matching, `%in%` is the way to go:

``x <- sample(1:4,10,replace=TRUE)x# [1] 3 4 3 3 2 3 1 1 2 2which(x %in% c(2,4))# [1]  2  5  9 10``

`%in%` returns a logical vector as long as the first argument, with a `TRUE` if that value can be found in the second argument and a `FALSE` otherwise.

## How to find indices of element in one vector in other vector R

``a <-  c('Q1', 'Q2', 'Q3')b <- c('Q10', 'Q13', 'Q1', 'Q1', 'Q40', 'Q2', 'Q2', 'Q2')which(b %in% a)[1] 3 4 6 7 8``

## Get index of a specific element in vector using %% operator

You can refer the left-hand side (lhs) of the pipe using the dot (`.`). There's two scenarios for this:

1. You want to use the lhs as an argument that is not in the first position. A common example is use of a `data` argument:

``mtcars %>% lm(mpg~cyl, data = .)``

In this case, `margrittr` will not inject the lhs into the first argument, but only in the argument marked with `.`.

2. You want to include the lhs not as a single function argument, but rather as part of an expression. This is your case! In this case `magrittr` will still inject the lhs as the first argument as well. You can cancel that with the curly braces (`{`).

So you need to use `.` notation with `{` braces:

``x %>% { which(. == "peach") }[1] 3``

Excluding the `{` would lead to trying to run the equivalent of `which(x, x == "peach")`, which yields an error.

## Finding the interval of the index of a vector in which values are increasing, decreasing, or remaining constant

You can use the base R `diff`, `sign` and `which` functions to identify the element pairs with sign change differences:

``x <- my_vectorz1 <- diff(x)z2 <- sign(z1)z3 <- diff(z2)no_change <- which(z3 == 0)no_changeminus_change <- which(z3 < 0)minus_change[1]  74 134plus_change <- which(z3 > 0)plus_change[1]   4 104``

In this case there are no zero sign changes. Note that for a vector of length n, the diff and sign vectors contain n-1 elements. So for example the minus_change values of 74, 134 represent the sign differences of the x[75:76] and x[135:136] pairs. See the help info for the R functions.

## Is the right function an R for finding row index of an elements from a vector in a data.frame?

``# Dummy datavector = c(1,2,10,400) # Vector of numbers want to find in dfdf = data.frame(data = seq(1,100,1), random = "yee") # dummy df # Loop to match vector numbers with data frame - on match save data frame rowgrab_row = list() # Initialize output listfor (i in 1:nrow(df)){  if(df\$data[i] %in% vector) {  # Check that any number in the vector is in the data frame column    grab_row[[i]] = df[i,] # if TRUE, grab the data frame row  } } # end# Output df with rows that matched vector out = do.call(rbind,grab_row)``

For the output

``   data random1     1    yee2     2    yee10   10    yee``

## Closest subsequent index for a specified value

Find the location of each value (numeric or character)

``int = c(1, 1, 0, 5, 2, 0, 0, 2)value = 0idx = which(int == value)## [1] 3 6 7``

Expand the index to indicate the nearest value of interest, using an NA after the last value in `int`.

``nearest = rep(NA, length(int))nearest[1:max(idx)] = rep(idx, diff(c(0, idx))),## [1]  3  3  3  6  6  6  7 NA``

Use simple arithmetic to find the difference between the index of the current value and the index of the nearest value

``abs(seq_along(int) - nearest)## [1]  2  1  0  2  1  0  0 NA``

Written as a function

``f <- function(x, value) {    idx = which(x == value)    nearest = rep(NA, length(x))    if (length(idx)) # non-NA values only if `value` in `x`        nearest[1:max(idx)] = rep(idx, diff(c(0, idx)))    abs(seq_along(x) - nearest)}``

We have

``> f(int, 0)[1]  2  1  0  2  1  0  0 NA> f(int, 1)[1]  0  0 NA NA NA NA NA NA> f(int, 2)[1] 4 3 2 1 0 2 1 0> f(char, "A")[1] 0 2 1 0 0> f(char, "B")[1]  1  0 NA NA NA> f(char, "C")[1]  2  1  0 NA NA``

The solution doesn't involve recursion or R-level loops, so should e fast even for long vectors.

## Finding the index values for a combination of vectors

Both conditions define logical vectors so AND them and get `which` indices they correspond to. Then plot.

I have added color to make the text labels more obvious.

``max_hat <- boxplot.stats(hat, coef = 2)\$stats[5]i <- abs(rs) > 1.96  # abs() because +/-1.96 are                      # symmetric valuesj <- hat > max_hatk <- which(i & j)plot(hat, rs, cex = 0.2+3*sqrt(x), pch = 19)text(hat[k], rs[k], labels = names(k), pos = 2, col = "red")``

## How to get the index of elements in a matrix that match values of a vector

Here's a function in base R to do this -

``match_a_row <- function(data, var1, var2) {  which(data[[1]] == var1 & data[[2]] == var2)}match_a_row(m, 'exponential', 'logit')#[1] 2match_a_row(m, 'independent', 'probit')#[1] 8``