简体   繁体   English

R如何创建包含列表列的数据框?

[英]R How do I create a dataframe containing a column of lists?

This is my current work around: 这是我目前的解决方法:

> df=data.frame(a=1)
> df$b = list(list(2,'a'))
> df
  a    b
1 1 2, a

It works, and I don't really mind that df=data.frame(a=1,b=list(list(1,'a'))) doesn't work . 它有效,我真的不介意df=data.frame(a=1,b=list(list(1,'a')))不起作用。

But referencing b requires [[]] notation, like this: df$b[[1]] . 但是引用b需要[[]]表示法,例如: df$b[[1]]

I'm looking for a solution that would allow simply df$b . 我正在寻找只允许使用df$b的解决方案。

What you're looking for is not possible. 您要找的东西是不可能的。 If you use lists, you will need to use the [[]] notation. 如果使用列表,则需要使用[[]]表示法。 Otherwise, you can transform the elements of df$b using the command unlist() . 否则,您可以使用unlist()命令转换df$b的元素。

> df$b
[[1]]
[[1]][[1]]
[1] 2

[[1]][[2]]
[1] "a" 

> unlist(df$b)
[1] "2" "a"
> unlist(df$b)[1]
[1] "2"
> unlist(df$b)[2]
[1] "a"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定带有 A 列的 R dataframe,如何创建两个包含 A 的所有有序组合的新列 - Given an R dataframe with column A, how do I create two new columns containing all ordered combinations of A 如何从一个列表中创建一个 dataframe,该列表的元素是包含一个 dataframe 的列表,每个 R - How to create one dataframe from a list whose elements are lists containing one dataframe each in R 如何基于列名对R中包含多个列表的列表进行子集化,并合并为单个列表/数据框? - How to subset a list containing several lists in R, based on the column name, and merge into a single list/dataframe? 创建一个包含另一个数据框列中唯一值计数的 R 数据框 - Create an R dataframe containing the counts of unique values in another dataframe column 如何在 Dataframe 中创建引用 R 中 Dataframe 中的值的列? - How Can I Create a Column in a Dataframe that References Values in the Dataframe in R? 在R中创建具有2个列表的数据框 - create a dataframe with 2 lists in R 如何使用R中的一列列表拆除数据框 - How to dismantle a dataframe with a column of lists in R 在R中:如何从字符串加上列名称再加上分类变量来创建数据框名称? - In R: How do I create a dataframe name from a string plus a column name plus categorical variable? 如何根据 R 中的另一个数据框创建条件列? - How do I create a conditional column based off another dataframe in R? 如何创建包含 dataframe 的所有数值的单列 dataframe 和多列? - How can I create a one-column dataframe containing all numeric values of a dataframe with several columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM