R中的数据框和列表之间有什么区别? [英] What is difference between dataframe and list in R?

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

问题描述

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

What is difference between dataframe and list in R? Which one should be used when? Which is easier to loop over?

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

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.

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

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"

推荐答案

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

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 :

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

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:

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

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

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

  • 您不能对两个不同的变量使用相同的名称
  • 数据帧的所有元素都是向量
  • 数据帧的所有元素都具有相等的长度.

由于这些限制以及由此产生的二维结构,数据帧可以模仿一些矩阵的行为.您可以选择行并对行进行操作.您无法使用列表来做到这一点,因为那里的行是未定义的.

Due to these restrictions and the resulting two-dimensional structure, data frames can mimick some 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.

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

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