R中的向量和列表数据类型之间有何区别? [英] What are the differences between vector and list data types in R?

查看:347
本文介绍了R中的向量和列表数据类型之间有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中的向量和列表数据类型之间的主要区别是什么?使用(或不使用)这两种数据类型的优点或缺点是什么?

What are the main differences between vector and list data types in R? What are the advantages or disadvantages of using (or not) these two data types?

我希望能看到一些示例来演示数据类型的用例.

I would appreciate seeing examples that demonstrate the use cases of the data types.

推荐答案

技术上列出了 are 向量,尽管很少有人会使用该术语. 列表"是几种模式之一,其他是逻辑",字符",数字",整数".用严格的R说法来说,您所说的向量是原子向量":

Technically lists are vectors, although very few would use that term. "list" is one of several modes, with others being "logical", "character", "numeric", "integer". What you are calling vectors are "atomic vectors" in strict R parlance:

 aaa <- vector("list", 3)
 is.list(aaa)   #TRUE
 is.vector(aaa)  #TRUE

列表是(向量的)递归"类型,而原子向量不是:

Lists are a "recursive" type (of vector) whereas atomic vectors are not:

is.recursive(aaa)  # TRUE
is.atomic(aaa)  # FALSE

根据数据对象是递归,原子还是具有维属性(矩阵和数组)来处理具有不同功能的数据对象.但是,我不确定对不同数据结构的优缺点"的讨论是否足以解决SO问题.为了补充汤米所说的内容,除了列表可以容纳任意数量的其他向量之外,还有数据帧的可用性,这些数据帧是具有定义其结构的维属性的列表的一种特殊类型.与实际上是折叠的原子对象的矩阵和数组不同,数据框可以包含各种类型,包括因子类型.

You process data objects with different functions depending on whether they are recursive, atomic or have dimensional attributes (matrices and arrays). However, I'm not sure that a discussion of the "advantages and disadvantages" of different data structures is a sufficiently focused question for SO. To add to what Tommy said, besides lists being capable of holding an arbitrary number of other vectors there is the availability of dataframes which are a particular type of list that has a dimensional attribute which defines its structure. Unlike matrices and arrays which are really folded atomic objects, dataframes can hold varying types including factor types.

还有一个警告,当存在除名称之外的其他属性时,is.vector函数将返回FALSE.请参阅:什么是向量?

There's also the caveat that the is.vector function will return FALSE when there are attributes other than names. See: what is vector?

这篇关于R中的向量和列表数据类型之间有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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