将向量与向量列表匹配 [英] Match a vector to a list of vectors

查看:77
本文介绍了将向量与向量列表匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量lis的列表,我需要将其与另一个向量vec

I have a list of vectors lis which I need to match to another vector vec

lis <- list(c(2,0,0),c(1,1,0), c(1,0,1), c(0,2,0), c(0,1,1), c(0,0,2))
vec <- c(1,1,0)

所以我要么得到一个逻辑输出

So either I would get a logical output

 [1] FALSE  TRUE  FALSE  FALSE  FALSE  FALSE

或者只是比赛lis内的位置

我一直在尝试以下方法:

I've been trying things along these lines:

unlist(lis) %in% vec

,但问题是数字的位置很重要,即区分我无法执行的c(1,1,0)c(1,0,1).我想避免for循环,因为这需要非常有效(快速).

but the problem is the position of the number is important i.e. distinguish between c(1,1,0) and c(1,0,1) which I haven't been able to do. I would like to avoid for loops as this needs to be quite efficient (fast).

推荐答案

sapply(lis,function(x)all(x==vec))
[1] FALSE  TRUE FALSE FALSE FALSE FALSE

这篇关于将向量与向量列表匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆