根据 R 中的命名匹配删除命名列表的元素

问题描述 投票:0回答:1

请注意:这个问题与上一个问题完全不同。

在下面的

My_list
中,我想知道如何消除名称前缀
"Gain"
附加了不同数字的整个元素?

例如,元素 #1 的名称是:

Gain1(conventional) - Gain2(conventional)
,因此,该元素必须是 REMOVED,因为我们有
Gain1
Gain2

但是元素 #2 的名称是:

Gain1(conventional) - Gain1(framework notes)
,因此,该元素必须是 KEPT,因为我们有
Gain1
Gain1

这在 R 中可能吗?

My_list <-

list(`Gain1(conventional) - Gain2(conventional)` = c(5L, -1L, 
-9L, 1L), `Gain1(conventional) - Gain1(framework notes)` = c(5L, 
-1L, -6L, 2L), `Gain1(conventional) - Gain2(framework notes)` = c(5L, 
-1L, -10L, 2L), `Gain1(conventional) - Gain1(note-taking instruction)` = c(5L, 
-1L, -7L, 3L), `Gain1(conventional) - Gain2(note-taking instruction)` = c(5L, 
-1L, -11L, 3L), `Gain1(conventional) - Gain1(vocabulary notebook)` = c(5L, 
-1L, -8L, 4L), `Gain1(conventional) - Gain2(vocabulary notebook)` = c(5L, 
-1L, -12L, 4L), `Gain2(conventional) - Gain1(framework notes)` = c(9L, 
-1L, -6L, 2L), `Gain2(conventional) - Gain2(framework notes)` = c(9L, 
-1L, -10L, 2L), `Gain2(conventional) - Gain1(note-taking instruction)` = c(9L, 
-1L, -7L, 3L), `Gain2(conventional) - Gain2(note-taking instruction)` = c(9L, 
-1L, -11L, 3L), `Gain2(conventional) - Gain1(vocabulary notebook)` = c(9L, 
-1L, -8L, 4L), `Gain2(conventional) - Gain2(vocabulary notebook)` = c(9L, 
-1L, -12L, 4L), `Gain1(framework notes) - Gain2(framework notes)` = c(6L, 
-2L, -10L, 2L), `Gain1(framework notes) - Gain1(note-taking instruction)` = c(6L, 
-2L, -7L, 3L), `Gain1(framework notes) - Gain2(note-taking instruction)` = c(6L, 
-2L, -11L, 3L), `Gain1(framework notes) - Gain1(vocabulary notebook)` = c(6L, 
-2L, -8L, 4L), `Gain1(framework notes) - Gain2(vocabulary notebook)` = c(6L, 
-2L, -12L, 4L), `Gain2(framework notes) - Gain1(note-taking instruction)` = c(10L, 
-2L, -7L, 3L), `Gain2(framework notes) - Gain2(note-taking instruction)` = c(10L, 
-2L, -11L, 3L), `Gain2(framework notes) - Gain1(vocabulary notebook)` = c(10L, 
-2L, -8L, 4L), `Gain2(framework notes) - Gain2(vocabulary notebook)` = c(10L, 
-2L, -12L, 4L), `Gain1(note-taking instruction) - Gain2(note-taking instruction)` = c(7L, 
-3L, -11L, 3L), `Gain1(note-taking instruction) - Gain1(vocabulary notebook)` = c(7L, 
-3L, -8L, 4L), `Gain1(note-taking instruction) - Gain2(vocabulary notebook)` = c(7L, 
-3L, -12L, 4L), `Gain2(note-taking instruction) - Gain1(vocabulary notebook)` = c(11L, 
-3L, -8L, 4L), `Gain2(note-taking instruction) - Gain2(vocabulary notebook)` = c(11L, 
-3L, -12L, 4L), `Gain1(vocabulary notebook) - Gain2(vocabulary notebook)` = c(8L, 
-4L, -12L, 4L))
r regex list function tidyverse
1个回答
1
投票

我们可以在

names(My_list)
上使用正则表达式并提取
Gain#

samegain <- strcapture("Gain([0-9]+).*Gain([0-9]+)", names(My_list), list(g1=0L, g2=0L)) |>
  with(g1 == g2)
samegain
#  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
# [17]  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
My_list[samegain]
# $`Gain1(conventional) - Gain1(framework notes)`
# [1]  5 -1 -6  2
# $`Gain1(conventional) - Gain1(note-taking instruction)`
# [1]  5 -1 -7  3
# $`Gain1(conventional) - Gain1(vocabulary notebook)`
# [1]  5 -1 -8  4
# $`Gain2(conventional) - Gain2(framework notes)`
# [1]   9  -1 -10   2
# $`Gain2(conventional) - Gain2(note-taking instruction)`
# [1]   9  -1 -11   3
# $`Gain2(conventional) - Gain2(vocabulary notebook)`
# [1]   9  -1 -12   4
# $`Gain1(framework notes) - Gain1(note-taking instruction)`
# [1]  6 -2 -7  3
# $`Gain1(framework notes) - Gain1(vocabulary notebook)`
# [1]  6 -2 -8  4
# $`Gain2(framework notes) - Gain2(note-taking instruction)`
# [1]  10  -2 -11   3
# $`Gain2(framework notes) - Gain2(vocabulary notebook)`
# [1]  10  -2 -12   4
# $`Gain1(note-taking instruction) - Gain1(vocabulary notebook)`
# [1]  7 -3 -8  4
# $`Gain2(note-taking instruction) - Gain2(vocabulary notebook)`
# [1]  11  -3 -12   4
© www.soinside.com 2019 - 2024. All rights reserved.