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

查看:41
本文介绍了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

但我得到了这个结果:

[1] FALSE FALSE

谁能告诉我,怎么了?

你好,卢皮

推荐答案

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

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天全站免登陆