过滤列表的有效方法

时间:2018-09-25 23:49:26

标签: r grep

我正在尝试根据特定条件提取词典单词列表的子集。

每个集合/向量中的n个选定单词必须与第1st个字符具有相同的3rd4thi字符

更具体地说,考虑列表

KHRSTT,  KHRSTK,  KKTLTK, TTLTTK, TKLTER

从此列表中,我想要一组所有1st3rd4th字符相同的单词

{
  {KHRSTT, KHRSTK},
  {TTLTTK, TKLTER}
}

{
  {KHRSTT, KHRSTK},
  {TTLTTK, TKLTER},
  {KKTLTK}
}

1 个答案:

答案 0 :(得分:2)

list  <- list('KHRSTT',  'KHRSTK',  'KKTLTK', 'TTLTTK', 'TKLTER')

a <- matrix("0",nrow = length(list),ncol= length(list))  

for (x in 1:length(list)){
    for (y in 1:length(list)){
        if( y != x){
            if(substr(list[x], start = 1, stop = 1) == substr(list[y], start = 1, stop = 1) &
               substr(list[x], start = 3, stop = 3) == substr(list[y], start = 3, stop = 3) &
               substr(list[x], start = 4, stop = 4) == substr(list[y], start = 4, stop = 4)){
                 a[x,x] = as.character(list[x])
                 a[x,y] = as.character(list[y])
                }
        }
    }
}

print(a)