list R中的数据框和列表有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15901224/
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:59:23  来源:igfitidea点击:

What is difference between dataframe and list in R?

rlistdataframe

提问by ShazSimple

What is difference between dataframeand listin R? Which one should be used when? Which is easier to loop over?

数据框列表中有什么区别R?应该在什么时候使用哪一种?哪个更容易循环?

Exact problem:I have to first store 3 string elements like "a", "b", "c". Later for each of these, I need to append 3 more elements; for instance for "a" I have to add "a1", "a2", "a3". Later I have to use nested for loops to access these elements.

确切的问题:我必须首先存储 3 个字符串元素,如“a”、“b”、“c”。稍后,对于其中的每一个,我都需要追加 3 个元素;例如对于“a”,我必须添加“a1”、“a2”、“a3”。后来我必须使用嵌套的 for 循环来访问这些元素。

So I am confused to use dataframe or list or some other data type, in which I could first store and then append (kind of each column)?

所以我很困惑使用数据框或列表或其他一些数据类型,我可以先存储然后追加(每列的种类)?

Currently I am getting errors, like "number of items to replace is not a multiple of replacement length"

目前我收到错误消息,例如“要替换的项目数不是替换长度的倍数”

回答by Joris Meys

The question isn't as stupid as some people think it is. I know plenty of people struggling with that difference, and what to use where. To summarize :

这个问题并不像某些人认为的那么愚蠢。我知道很多人都在为这种差异而苦苦挣扎,以及在何处使用什么。总结一下:

Lists are by far the most flexible data structure in R. They can be seen as a collection of elements without any restriction on the class, length or structure of each element. The only thing you need to take care of, is that you don't give two elements the same name. That might cause a lot of confusion, and R doesn't give errors for that:

列表是迄今为止 R 中最灵活的数据结构。它们可以被视为元素的集合,对每个元素的类、长度或结构没有任何限制。您唯一需要注意的是,不要给两个元素赋予相同的名称。这可能会引起很多混乱,而 R 不会为此给出错误:

> X <- list(a=1,b=2,a=3)
> X$a
[1] 1

Data frames are lists as well, but they have a few restrictions:

数据框也是列表,但它们有一些限制:

  • you can't use the same name for two different variables
  • all elements of a data frame are vectors
  • all elements of a data frame have an equal length.
  • 您不能对两个不同的变量使用相同的名称
  • 数据框的所有元素都是向量
  • 数据帧的所有元素都具有相等的长度。

Due to these restrictions and the resulting two-dimensional structure, data frames can mimicksome of the behaviour of matrices. You can select rows and do operations on rows. You can't do that with lists, as a row is undefined there.

由于这些限制和由此产生的二维结构,数据框可以模仿矩阵的某些行为。您可以选择行并对行进行操作。你不能用列表来做到这一点,因为那里有一行是未定义的。

All this implies that you should use a data frame for any dataset that fits in that twodimensional structure. Essentially, you use data frames for any dataset where a column coincides with a variable and a row coincides with a single observation in the broad sense of the word. For all other structures, lists are the way to go.

所有这些都意味着您应该为适合该二维结构的任何数据集使用数据框。从本质上讲,您可以将数据框用于任何数据集,其中列与变量重合,行与广义上的单个观察重合。对于所有其他结构,列表是可行的方法。

Note that if you want a nested structure, you have to use lists. As elements of a list can be lists themselves, you can create very flexible structured objects.

请注意,如果您想要嵌套结构,则必须使用列表。由于列表的元素本身可以是列表,因此您可以创建非常灵活的结​​构化对象。

回答by user5220347

Look at the example: If you use apply instead of sapply to get the class -

看这个例子:如果你使用 apply 而不是 sapply 来获取类 -

apply(iris,2,class) #  function elements are rows or columns
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
"character"  "character"  "character"  "character"  "character" 

sapply(iris,class) # function elements are variables
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
"numeric"    "numeric"    "numeric"    "numeric"     "factor"