list R 中的向量和列表数据类型有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8594814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:45:30  来源:igfitidea点击:

What are the differences between vector and list data types in R?

listrvector

提问by DCR

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?

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

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

我很高兴看到演示数据类型用例的示例。

回答by IRTFM

Technically lists arevectors, 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:

从技术上讲,列表向量,尽管很少有人会使用该术语。“列表”是几种模式之一,其他模式是“逻辑”、“字符”、“数字”、“整数”。你所说的向量是严格的 R 语言中的“原子向量”:

 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

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.

您可以使用不同的函数处理数据对象,具体取决于它们是递归的、原子的还是具有维度属性(矩阵和数组)。但是,我不确定对不同数据结构的“优点和缺点”的讨论是否对 SO 来说是一个足够集中的问题。补充一下 Tommy 所说的,除了列表能够保存任意数量的其他向量之外,还有数据帧的可用性,数据帧是一种特定类型的列表,具有定义其结构的维度属性。与真正折叠原子对象的矩阵和数组不同,数据帧可以包含不同的类型,包括因子类型。

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

还需要注意的是,当存在名称以外的属性时,该is.vector函数将返回FALSE。请参阅:什么是矢量?

回答by Tommy

Lists are "recursive". This means that they can contain values of different types, even other lists:

列表是“递归的”。这意味着它们可以包含不同类型的值,甚至其他列表:

x <- list(values=sin(1:3), ids=letters[1:3], sub=list(foo=42,bar=13))
x # print the list
x$values   # Get one element
x[["ids"]] # Another way to get an element
x$sub$foo  # Get sub elements
x[[c(3,2)]]  # Another way (gets 13)
str(x)     # A "summary" of the list's content

Lists are used in R to represent data sets: the data.frameclass is essentially a list where each element is a column of a specific type.

R 中使用列表来表示数据集:data.frame类本质上是一个列表,其中每个元素都是特定类型的列。

Another use is when representing a model: the result from lmreturns a list that contains a bunch of useful objects.

另一个用途是表示模型时:结果 fromlm返回一个包含一堆有用对象的列表。

d <- data.frame(a=11:13, b=21:23)
is.list(d) # TRUE
str(d)

m <- lm(a ~ b, data=d)
is.list(m) # TRUE
str(m)

Atomic vectors (non-list like, but numeric, logical and character) are useful since all elements are known to have the same type. This makes manipulating them very fast.

原子向量(非列表,但数字、逻辑和字符)很有用,因为已知所有元素都具有相同的类型。这使得操纵它们的速度非常快。

回答by Andy V

As someone who's just gotten into R, but comes from a C/Java/Ruby/PHP/Python background, here's how I think of it.

作为一个刚刚接触 R,但来自 C/Java/Ruby/PHP/Python 背景的人,这就是我的想法。

A listis really an array + a hashmap. It's a PHP associative array.

Alist实际上是一个数组 + 一个哈希图。它是一个 PHP 关联数组。

> foo = list(bar='baz')
> foo[1]
'baz'
> foo$bar
'baz'
> foo[['bar']]
'baz'

A vectoris a fixed-type array/list. Think of it like a linked list - because putting dissimilar items into a linked list is an anti-pattern anyways. It's a vector in the same sense that SIMD/MMX/vector units use the word.

Avector是固定类型的数组/列表。把它想象成一个链表——因为将不同的项目放入链表是一种反模式。它是一个向量,与 SIMD/MMX/向量单元使用这个词的意义相同。

回答by Patrick Burns

This and similar introductory questions are answered in http://www.burns-stat.com/pages/Tutor/hints_R_begin.html

这个和类似的介绍性问题在http://www.burns-stat.com/pages/Tutor/hints_R_begin.html 中有回答

It is meant to be a gentle introduction that gets you up and running with R as quickly as possible. To some extent it succeeds.

它是一个温和的介绍,可以让您尽快开始使用 R。在某种程度上它成功了。

--- Edit: --

- - 编辑: -

An attempt to explain further; quoted from the above reference.

试图进一步解释;引用自上述参考。

Atomic vector

There are three varieties of atomic vector that you are likely to encounter:

  • “numeric”
  • “logical”
  • “character”

The thing to remember about atomic vectors is that all of the elements in them are only of one type.

List

Lists can have different types of items in different components. A component of a list is allowed to be another list , an atomic vector (and other things).

原子向量

您可能会遇到三种原子向量:

  • “数字”
  • “合乎逻辑”
  • “特点”

关于原子向量要记住的是,它们中的所有元素都只有一种类型。

列表

列表在不同的组件中可以有不同类型的项目。列表的一个组件可以是另一个列表,一个原子向量(和其他东西)。

Please also refer to thislink.

另请参阅链接。

回答by Devyani Balyan

list include multiple data types like character, numeric, logical et. but vector only contains similar type of data. for ex:

列表包括多种数据类型,如字符、数字、逻辑等。但是 vector 只包含相似类型的数据。例如:

scores <- c(20,30,40,50)
student <- c("A","B","C","D")
sc_log <- c(TRUE,FALSE,FALSE,TRUE)

for list:

清单:

mylist <- list(scores,student,sc_log)
# search for class of mylist vector 
#check structure of mylist using str() function.
str(mylist)
[1] list of 3
[1] $:num [1:4] 20 30 40 50
[2] $:chr [1:4] "A""B""C""D"
[3] $:log [1:4] TRUE FALSE FALSE TRUE

which means list containing multiple data types like numeric, character and logical in mylist.But in vector there will be single data type of all elements in that vector

这意味着列表在 mylist 中包含多种数据类型,如数字、字符和逻辑。但在向量中,该向量中的所有元素将具有单一数据类型

for ex:

例如:

for vector:

对于向量:

vector1 <- c(1,2,3,4)
Class(vector1)
[1] "Numeric"

#which means all elements of vector containing single data type that is numeric only.