[英]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.