我正在尝试根据特定条件提取词典单词列表的子集。
每个集合/向量中的n
个选定单词必须与第1st
个字符具有相同的3rd
,4th
和i
字符
更具体地说,考虑列表
KHRSTT, KHRSTK, KKTLTK, TTLTTK, TKLTER
从此列表中,我想要一组所有1st
,3rd
,4th
字符相同的单词
{
{KHRSTT, KHRSTK},
{TTLTTK, TKLTER}
}
或
{
{KHRSTT, KHRSTK},
{TTLTTK, TKLTER},
{KKTLTK}
}
答案 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)