如何在R中移除名称前缀Gain数字不同的命名列表元素?
问题描述
在下方的My_list中,需要移除所有名称里前缀"Gain"后附带数字不同的元素:
- 例如元素名称
Gain1(conventional) - Gain2(conventional),因为Gain1和Gain2的数字不同,需要移除; - 而元素名称
Gain1(conventional) - Gain1(framework notes),因为前后数字都是1,需要保留。
请问在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))
解决方案
可以通过提取列表名称中Gain后的数字,筛选出前后数字一致的元素,提供两种实现方式:
方式一:使用stringr包(简洁直观)
library(stringr) # 提取每个列表名称中的两个Gain数字 gain_nums <- str_extract_all(names(My_list), "(?<=Gain)\\d+", simplify = TRUE) # 筛选出前后数字相等的元素 filtered_list <- My_list[gain_nums[, 1] == gain_nums[, 2]]
方式二:基础R实现(无需额外包)
# 提取每个列表名称中的两个Gain数字 gain_nums <- do.call(rbind, regmatches(names(My_list), gregexpr("(?<=Gain)\\d+", names(My_list), perl = TRUE))) # 筛选出前后数字相等的元素 filtered_list <- My_list[gain_nums[, 1] == gain_nums[, 2]]
代码说明
- 正则表达式
(?<=Gain)\\d+用于精准提取紧跟在Gain后的数字,(?<=Gain)是正向先行断言,确保只匹配Gain之后的数字部分; - 提取结果为二维数组,每行对应一个列表名称的两个
Gain数字; - 通过比较每行的两个数字是否相等,保留符合条件的列表元素。
内容的提问来源于Stack Exchange,提问作者Simon Harmel
相关产品推荐
相关产品推荐

