使用逗号创建数据框
是否可以制作一个包含“多个元素”列的数据框?
例如 - 给定以下数据:
a = sample(c(1,-1), size=2 ,replace = T, prob=c(0.5, 0.5))
b = sample(c(1,-1), size=3 ,replace = T, prob=c(0.5, 0.5))
c = sample(c(1,-1), size=4 ,replace = T, prob=c(0.5, 0.5))
#some random numbers
d = rexp(3,5)
#some random letters
e = "g"
#id column
n_id = 1:3
所有这些都可以组合成一个数据框(4 列,3 行)吗?我尝试以常规方式执行此操作:
answer = data.frame(a,b,c,d,e)
但出现此错误:
Error in data.frame(a, b, c, d, e, n_id) :
arguments imply differing number of rows: 2, 3, 4, 1
Is it possible to do this in R?我试图得到这样的东西:
谢谢!
Is it possible to make a data frame containing a column with "multiple elements"?
For instance - given the following data:
a = sample(c(1,-1), size=2 ,replace = T, prob=c(0.5, 0.5))
b = sample(c(1,-1), size=3 ,replace = T, prob=c(0.5, 0.5))
c = sample(c(1,-1), size=4 ,replace = T, prob=c(0.5, 0.5))
#some random numbers
d = rexp(3,5)
#some random letters
e = "g"
#id column
n_id = 1:3
Can all this be combined into a single data frame (4 columns, 3 rows)? I tried to do this the regular way:
answer = data.frame(a,b,c,d,e)
But I get this error:
Error in data.frame(a, b, c, d, e, n_id) :
arguments imply differing number of rows: 2, 3, 4, 1
Is it possible to do this in R? I am trying to get something like this:
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用 tidyverse
注意,在这两种方法中
a
是一个列表列You can also do with tidyverse
Notice under both approaches that
a
is a list-column您可以使用此代码:
随着输出:
它会发出警告,因为您实际上不希望数据帧具有不同的向量长度。
You can use this code:
With output:
It gives an warning because you actually don't want dataframes with different vector lengths.