在R中,如何检查列表是否包含特定键?

时间:2021-02-18 06:59:12

Suppose I have a list as follows

假设我有一个如下列表

foo=list(bar="hello world")

I would like to check whether my list has a particular key. I observe foo$bar2 will return NULL for any bar2 that is not equal to bar, so I figured I could check for whether the return value was null, but this does not seem to work:

我想检查一下我的列表是否有特定的密钥。我观察foo $ bar2将为任何不等于bar的bar2返回NULL,所以我想我可以检查返回值是否为null,但这似乎不起作用:

if (foo$bar2==NULL) 1 # do something here

However, this gives the error:

但是,这会给出错误:

Error in if (foo$bar2 == NULL) 1 : argument is of length zero

I then tried whether NULL is equivalent to false, like in C:

然后我尝试NULL是否等于false,就像在C中一样:

if (foo$bar2) 1 # do something here

This gives the same error.

这给出了同样的错误。

I now have two questions. How can I check whether the list contains the key? And how do I check whether an expression is null?

我现在有两个问题。如何检查列表中是否包含密钥?我如何检查表达式是否为空?

1 个解决方案

#1


20  

The notion of "keys" are called "names" in R.

“键”的概念在R中称为“名称”。

if ("bar" %in% names(foo) ) {  print("it's there") }  # ....

They are stored in a special attribute named .Names and extracted with the names function:

它们存储在名为.Names的特殊属性中,并使用names函数提取:

dput(foo)
#structure(list(bar = "hello world"), .Names = "bar")

I offer a semantic caution here, because of a common source of confusion due to two distinct uses of the word: "names" in R: There are .Names-attributes, but there is an entirely different use of the word name in R that has to do with strings or tokens that have values independent of any inspection or extraction functions like $ or [. Any token that starts with a letter or a period and has nor other special characters in it can be a valid name. One can test for it with the the function exists given a quoted version of its name:

我在这里提出了一个语义上的谨慎,因为这个词的两个不同用法是混淆的常见原因:R中的“names”:有.Names属性,但R中的单词名称有完全不同的用法与字符串或标记有关,这些字符串或标记的值独立于任何检查或提取函数,如$或[。任何以字母或句点开头并且其中包含其他特殊字符的标记都可以是有效名称。在给定其名称的引用版本的情况下,可以使用函数来测试它:

 exists("foo")  # TRUE
 exists(foo$bar) #    [1] FALSE
 exists("foo$bar")#    [1] FALSE

So the word name has two different meanings in R and you will need to be aware of this ambiguity to understand how the language is deployed. The .Names meaning refers to an attribute with special purposes, while the names-meaning refers to what is called a "language-object". The word symbol is a synonym for this second meaning of the word.

因此,单词名称在R中有两个不同的含义,您需要了解这种歧义,以了解语言的部署方式。 .Names含义是指具有特殊用途的属性,而名称含义是指所谓的“语言对象”。单词符号是该单词的第二个含义的同义词。

is.name( quote(foo) ) #[1] TRUE

To then show how your second question about testing for nullity might flow into this :

然后展示你关于测试无效的第二个问题可能会如何:

if( !is.null(foo$bar) ) {  print("it's there") }  # any TRUE value will be a 1

#1


20  

The notion of "keys" are called "names" in R.

“键”的概念在R中称为“名称”。

if ("bar" %in% names(foo) ) {  print("it's there") }  # ....

They are stored in a special attribute named .Names and extracted with the names function:

它们存储在名为.Names的特殊属性中,并使用names函数提取:

dput(foo)
#structure(list(bar = "hello world"), .Names = "bar")

I offer a semantic caution here, because of a common source of confusion due to two distinct uses of the word: "names" in R: There are .Names-attributes, but there is an entirely different use of the word name in R that has to do with strings or tokens that have values independent of any inspection or extraction functions like $ or [. Any token that starts with a letter or a period and has nor other special characters in it can be a valid name. One can test for it with the the function exists given a quoted version of its name:

我在这里提出了一个语义上的谨慎,因为这个词的两个不同用法是混淆的常见原因:R中的“names”:有.Names属性,但R中的单词名称有完全不同的用法与字符串或标记有关,这些字符串或标记的值独立于任何检查或提取函数,如$或[。任何以字母或句点开头并且其中包含其他特殊字符的标记都可以是有效名称。在给定其名称的引用版本的情况下,可以使用函数来测试它:

 exists("foo")  # TRUE
 exists(foo$bar) #    [1] FALSE
 exists("foo$bar")#    [1] FALSE

So the word name has two different meanings in R and you will need to be aware of this ambiguity to understand how the language is deployed. The .Names meaning refers to an attribute with special purposes, while the names-meaning refers to what is called a "language-object". The word symbol is a synonym for this second meaning of the word.

因此,单词名称在R中有两个不同的含义,您需要了解这种歧义,以了解语言的部署方式。 .Names含义是指具有特殊用途的属性,而名称含义是指所谓的“语言对象”。单词符号是该单词的第二个含义的同义词。

is.name( quote(foo) ) #[1] TRUE

To then show how your second question about testing for nullity might flow into this :

然后展示你关于测试无效的第二个问题可能会如何:

if( !is.null(foo$bar) ) {  print("it's there") }  # any TRUE value will be a 1