【发布时间】:2016-05-27 22:18:38
【问题描述】:
我有一个包含 S4 对象的列表。我需要检查该列表中是否存在一个 S4 对象。我尝试了在此页面中找到的一些替代方法,但均未成功:
我尝试过存在:
exists(foo,where=my_list)
Error in list2env(list(<S4 object of class "Atributo">, <S4 object of class "Atributo">,
;names(x) must be a character vector of the same length as x
搭配:
match(foo, my_list)
'match' requires vector arguments
match的二元运算符给出相同的结果%in%:
foo %in% my_list
'match' requires vector arguments
而is.element:
is.element(foo,my_list)
Error in match(el, set, 0L) : 'match' requires vector arguments
例如,我创建了 5 个元素并制作了一个列表,其中只有 3 个。我需要知道列表中是否有一个特定元素:
setClass("foo", representation = representation(bar = "numeric"))
one <- new("foo", bar = 12)
two <- new("foo", bar = 13)
three <- new("foo", bar = 14)
four <- new("foo", bar = 15)
five <- new("foo", bar = 16)
mylist <- list(one,two,three)
我想检查列表中是否存在特定元素,例如:
usefull_function(four,my_list)
FALSE
usefull_function(two,my_list)
TRUE
我知道它们是否是 S4 元素,我可以检查环境是否存在于环境列表中。 ¿ 怎样才能优雅/快速地做到这一点?
谢谢。
【问题讨论】: