在R中的数据框架元素中存储列表 [英] Storing a list within a data frame element in R

查看:141
本文介绍了在R中的数据框架元素中存储列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以

Start, End, Elements
  3  , 6  ,  {4,5}
  4 ,  10 ,  {7,8,9}
   ....

说到这里,我沿着一条线移动一个球。 开始表示球的最左侧位置,End表示最右侧的位置。 元素意味着我不知何故找到这些职位。当元素数量增长非常大时,使用最好的数据结构是什么?我唯一可以想到的是第三列是一个格式正确的字符串的数据帧。如果我想查看该集合中的每个数字,我将必须解析该字符串。 R有没有更好的数据格式?

In words, I am moving a ball along a line. The "start" represents the left most position of the ball and the "End" represents the right most. The "Elements" means I somehow find those positions special. What is the best data structure to use when the number of elements can grow very large? The only thing I can think of is a data frame where the 3rd column is an appropriately formatted string. I would then have to parse the string if I wanted to look at each number in the set. Is there a better data format that R has or is that about it?

谢谢!

推荐答案

我的评论中提到的选项,即只需使用一列列表:

The option mentioned in my comment, i.e. simply using a list for one of the columns:

dat <- data.frame(Start = 3:4, End = c(6,10))
> dat
  Start End
1     3   6
2     4  10
> dat$Elements <- list(4:5,7:9)
> dat
  Start End Elements
1     3   6     4, 5
2     4  10  7, 8, 9

您还可以完全使用数据框,只需使用一个简单的旧列表(在许多情况下,这可能更有意义):

You could also of course ditch data frames entirely and simply use a plain old list (which might make more sense in a lot of cases, anyway):

list(list(Start = 3,End = 6, Elements = 4:5),list(Start = 4,End = 10,Elements = 7:9))
[[1]]
[[1]]$Start
[1] 3

[[1]]$End
[1] 6

[[1]]$Elements
[1] 4 5


[[2]]
[[2]]$Start
[1] 4

[[2]]$End
[1] 10

[[2]]$Elements
[1] 7 8 9

这篇关于在R中的数据框架元素中存储列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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