在列表中显示相邻成员

发布于 2025-01-23 07:35:39 字数 292 浏览 1 评论 0原文

我想根据匹配项检查列表中的相邻元素。例如,在随机订购的字母列表中,我想知道m的相邻字母是什么。我当前的解决方案是:

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

对我来说,str_which()的重复感到不必要。是否有更简单的方法可以达到相同的结果?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2025-01-30 07:35:39

首先,我用种子再生数据可重复性:

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"

使用-2:2,然后(警告性地)删除矢量长度以下的那些:

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

如果您的目标是一个以上(不是一个 以上)只是“ m”),然后我们可以使用其他方法。

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"

或者,如果您不在乎将它们分组,我们可以尝试以下操作:

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"

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"
余生一个溪 2025-01-30 07:35:39

如果您没有重复项,则可以像以下

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

那样尝试代码

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

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, `+`))]
一曲琵琶半遮面シ 2025-01-30 07:35:39

您还可以使用slider :: slide函数(使用 @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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文