得到一个包含所有“变量”的向量;“真的”;R语言中列表中的引用

得到一个包含所有“变量”的向量;“真的”;R语言中列表中的引用,r,list,vector,R,List,Vector,我正在做文本挖掘,我需要一个最终的向量(出现次数),以便对出现在其他向量上的单词只有一个为真。 例如: 从这个向量test=c(“1”、“2”、“4”、“6”) 以及下列名单: list_test = list(y ,x, "1") 我需要以下事件: print(occurrences) TRUE TRUE TRUE # but I'm getting > print(occurrences) [1] TRUE TRUE 算法哪里出错了 test = c("1","2","4","

我正在做文本挖掘,我需要一个最终的向量(出现次数),以便对出现在其他向量上的单词只有一个为真。 例如:

从这个向量
test=c(“1”、“2”、“4”、“6”)

以及下列名单:

list_test = list(y ,x, "1")
我需要以下事件:

print(occurrences)
TRUE TRUE TRUE

# but I'm getting 
> print(occurrences)
[1] TRUE TRUE
算法哪里出错了

test = c("1","2","4","6")
occurrences = c()
double_occurrences = c()
all_occurrences = c()

y = c("5","2")
x = c("3","2", "2", "4", "3", "1")

list_test = list(y ,x, "1")

for(comment_test in test){
  for(testword in list_test){    
    if (length(testword) > 1){
      for(i in 1: length(testword)){
        for (keyword in grepl(comment_test, testword[i], fixed=TRUE)){
          if (keyword == TRUE){
            double_occurrences = c(double_occurrences, keyword) 
          }
        }
      }
  } else {
    keyword = grepl(comment_test, testword, fixed=TRUE)
      if (keyword == TRUE){
        occurrences = c(occurrences, keyword) 
        }
      }
  }
  if((length(double_occurrences)) > 1){ # To several lists of words just ONE TRUE
    double_occurrences =  c(TRUE)
    all_occurrences = c(all_occurrences, double_occurrences)
    double_occurrences = c()
  }
}

occurrences = c(occurrences, all_occurrences)
print(occurrences)
实际输出为

print(occurrences)
TRUE TRUE
并期望

print(occurrences)
TRUE TRUE TRUE
这个怎么样:

test %in% unlist(list_test)
#[1]  TRUE  TRUE  TRUE FALSE

由于%运算符中的
%已矢量化,因此不需要
for
循环。输出是一个与
test
长度相同的布尔向量,如果
test
中的相应元素位于
列表测试中,则其每个元素都是
TRUE
,否则为
FALSE

我认为你的问题不清楚。在您的预期输出中,每个
TRUE
是否对应于
列表中的每个测试向量?i、 e.如果
test
的字母出现在
list\u test
的第一个元素中,则返回
TRUE
?出现在
list\u test
上的
TRUE
值。如果向量
test
的字符串出现在
list\u test
的元素中,则出现的
应该为每个元素返回一个
TRUE
。例如:向量
test
的字符串“1”出现在向量
x
上的
list\u test
中,也出现在“1”中,然后它应该返回一个
TRUE
。在代码之后,计算字符串“2”,它出现在向量
y
x
上,因此应返回另一个
TRUE
,依此类推字符串“4”。
test %in% unlist(list_test)
#[1]  TRUE  TRUE  TRUE FALSE