简体   繁体   English

在R中使用lapply和str

[英]Using lapply and str in R

When I input the following command in R. 当我在R中输入以下命令时。

lapply(list(1,TRUE),str)

It displays the following results: 它显示以下结果:

num 1

 logi TRUE

[[1]]

NULL


[[2]]

NULL

I know num 1 and logi TRUE are the structure of each element. 我知道num 1logi TRUE是每个元素的结构。 What is the meaning of the following part? 下一部分是什么意思?

[[1]]

NULL


[[2]]

NULL

This happens because the return value of str is NULL . 发生这种情况是因为str的返回值为NULL Consider: 考虑:

a <- str(list(1,TRUE))
a # NULL

Now because you use lapply , lapply will return a list with an equal number of elements as the input list. 现在,因为您使用lapply ,所以lapply将返回一个列表,该列表具有与输入列表相同数量的元素。 In your case, this is a list of two elements that are both NULL . 在您的情况下,这是两个均为NULL元素的列表。

The [[N]] output means [[N]]输出表示

  1. The first element of this list is a vector with a single element NULL 此列表的第一个元素是带有单个元素NULL的向量

  2. the second element of this list is a vector with a single element NULL 此列表的第二个元素是带有单个元素NULL的向量

Appending the [[N]] to the variable that holds the list actually returns the element. [[N]]附加到保存列表的变量实际上返回该元素。

In addition to the previous answers, it could be useful to use 除了先前的答案,使用它可能会很有用

invisible(lapply(list(dt1,dt2...), str)) 

so you don't print anything more than the structure of the objects. 因此,您仅需打印对象的结构即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM