4

I want to inspect adjacent elements in a list based on a match. For example, in a list of randomly ordered letters, I want to know what the neighboring letters of m is. My current solution is:

library(stringr)
ltrs <- sample(letters)
ltrs[(str_which(ltrs,'m')-2):(str_which(ltrs,'m')+2)]
[1] "j" "f" "m" "q" "a"

To me, the repetition of str_which() feels unnecessary. Is there a simpler way to achieve the same result?

user438383
  • 5,716
  • 8
  • 28
  • 43
msn
  • 113
  • 3

3 Answers3

5

First, I regenerate random data with a seed for reproducibility:

set.seed(42)
ltrs <- sample(letters)
ltrs
#  [1] "q" "e" "a" "j" "d" "r" "z" "o" "g" "v" "i" "y" "n" "t" "w" "b" "c" "p" "x"
# [20] "l" "m" "s" "u" "h" "f" "k"

Use -2:2 and then (cautionarily) remove those below 1 or above the length of the vector:

ind <- -2:2 + which(ltrs == "m")
ind <- ind[0 < ind & ind < length(ltrs)]
ltrs[ind]
# [1] "x" "l" "m" "s" "u"

If your target is more than one (not just "m"), then we can use a different approach.

ind <- which(ltrs %in% c("m", "f"))
ind <- lapply(ind, function(z) { z <- z + -2:2; z[0 < z & z <= length(ltrs)]; })
ind
# [[1]]
# [1] 19 20 21 22 23
# [[2]]
# [1] 23 24 25 26
lapply(ind, function(z) ltrs[z])
# [[1]]
# [1] "x" "l" "m" "s" "u"
# [[2]]
# [1] "u" "h" "f" "k"

Or, if you don't care about keeping them grouped, we can try this:

ind <- which(ltrs %in% c("m", "f"))
ind <- unique(sort(outer(-2:2, ind, `+`)))
ind <- ind[0 < ind & ind <= length(ltrs)]
ltrs[ind]
# [1] "x" "l" "m" "s" "u" "h" "f" "k"
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • What's the reason for the additional step in the first answer in terms of being cautious? Is there a potential issue if you were to do `ltrs[-2:2 + which(ltrs == "m")]`? I get the same answer, so just curious of potential problems. – AndrewGB Apr 22 '22 at 05:32
  • 2
    Sure. If we try to match `"k"` with `ltrs[-2:2 + which(ltrs == "k")]`, we'll get `c("h", "f", "k", NA, NA)`. Another way to safeguard against getting `NA`s would be to wrap it in `na.omit`. There are several ways. – r2evans Apr 22 '22 at 05:51
3

If you don't have duplicates, you can try the code like below

ltrs[seq_along(ltrs)%in% (which(ltrs=="m")+(-2:2))]

otherwise

ltrs[seq_along(ltrs) %in% c(outer(which(ltrs == "m"), -2:2, `+`))]
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

You can also use the slider::slide function (using data provided by @r2evans):

slider::slide(ltrs, ~ .x, .before = 2, .after = 2)[[which(ltrs == "m")]]
# [1] "x" "l" "m" "s" "u"

slider::slide(ltrs, ~ .x, .before = 2, .after = 2)[which(ltrs %in% c("m","f"))]
# [[1]]
# [1] "x" "l" "m" "s" "u"
#
# [[2]]
# [1] "u" "h" "f" "k"
Maël
  • 45,206
  • 3
  • 29
  • 67