R:在向量列表中找到向量 [英] R: find vector in list of vectors

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

问题描述

我正在使用R,我的目标是检查给定向量是否在唯一向量列表中.

i'm working with R and my goal is to check wether a given vector is in a list of unique vectors.

列表看起来像

final_states <- list(c("x" = 5, "y" = 1),
                 c("x" = 5, "y" = 2),
                 c("x" = 5, "y" = 3),
                 c("x" = 5, "y" = 4),
                 c("x" = 5, "y" = 5),
                 c("x" = 3, "y" = 5))

现在,我想检查列表中是否存在给定状态.例如:

Now I want to check wether a given state is in the list. For example:

state <- c("x" = 5, "y" = 3)

如您所见,向量状态是final_states列表的元素.我的想法是使用%in%运算符进行检查:

As you can see, the vector state is an element of the list final_states. My idea was to check it with %in% operator:

state %in% final_states

但是我得到了这个结果:

But I get this result:

[1] FALSE FALSE

谁能告诉我,怎么了?

Can anyone tell me, what is wrong?

问候, 卢比

推荐答案

如果只想确定向量是否在列表中,请尝试

If you just want to determine if the vector is in the list, try

Position(function(x) identical(x, state), final_states, nomatch = 0) > 0
# [1] TRUE

Position()基本上与match()相似,但是在列表上.如果设置nomatch = 0并检查Position > 0,则会得到一个逻辑结果,告诉您state是否在final_states

Position() basically works like match(), but on a list. If you set nomatch = 0 and check for Position > 0, you'll get a logical result telling you whether state is in final_states

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

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