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

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

推荐答案

从技术上讲,列表向量,尽管很少有人会使用该术语.列表"是几种模式之一,其他模式是逻辑"、字符"、数字"、整数".在严格的 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 来说是一个足够集中的问题.补充一下 Tommy 所说的,除了列表能够保存任意数量的其他向量之外,还有数据帧的可用性,数据帧是一种特定类型的列表,具有定义其结构的维度属性.与真正折叠原子对象的矩阵和数组不同,数据帧可以包含各种类型,包括因子类型.

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