如何在R中将数组转换为宽数据帧? [英] How do I convert an array to a wide data frame in R?
本文介绍了如何在R中将数组转换为宽数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个数组
arr = as.array(c(1.1,0.5,3.2,4.3,5.5,6.3,0.4))
我想将其转换为数据框,所以我尝试:
I want to convert this into a data frame so I try:
df = as.data.frame(arr)
但是这给了我一个nrows = 7和ncols = 1的数据框。我需要一个nrows = 1和ncols = 7的数据框。我尝试做 t(df)
,但这将返回一个矩阵。
But this gives me a data frame with nrows = 7 and ncols = 1. I need a data frame with nrows = 1 and ncols = 7. I have tried doing t(df)
but that would return a matrix.
我知道必须有一种简单的方法来执行此操作……谢谢!
I know there has to be a simple way to do this... thank you!
推荐答案
如何操作:
arr = array(c(1.1,0.5,3.2,4.3,5.5,6.3,0.4), dim = c(1, 7))
as.data.frame(arr)
# V1 V2 V3 V4 V5 V6 V7
#1 1.1 0.5 3.2 4.3 5.5 6.3 0.4
注意,使用 array
或 matrix
,而不是 as.array
或 as.matrix
。
Note, use array
or matrix
, not as.array
or as.matrix
.
alistaire 提到了一个好方法
as.data.frame(t(c(1.1,0.5,3.2,4.3,5.5,6.3,0.4)))
# V1 V2 V3 V4 V5 V6 V7
#1 1.1 0.5 3.2 4.3 5.5 6.3 0.4
这将自动为您转换昏暗
。
这篇关于如何在R中将数组转换为宽数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文