r - 使用 R 将 DataFrame 展平为单行

标签 r

我想使用 R 重组以下多行 DataFrame,

       1          2       3
A  Apple     Orange   Grape
B    Car      Truck   Plane
C  House  Apartment  Garage

进入这个单行 DataFrame。

     1_A     2_A    3_A  1_B    2_B    3_B    1_C        2_C     3_C
0  Apple  Orange  Grape  Car  Truck  Plane  House  Apartment  Garage

感谢您的帮助!

最佳答案

reshape 解决方案,一步处理所有命名和重命名:

reshape(cbind(dat, id=1, time=rownames(dat)),
        direction="wide", sep="_", new.row.names=0)[-1]
#    1_A    2_A   3_A 1_B   2_B   3_B   1_C       2_C    3_C
#0 Apple Orange Grape Car Truck Plane House Apartment Garage

关于r - 使用 R 将 DataFrame 展平为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69184293/

相关文章:

R 迭代数据框以添加具有连续值的新列

r - R中的颜色强度误差

r - 你如何在 Rmarkdown 中制作一个漂亮的表格

java - 在 RHEL 6 上安装 R R-java-devel 需要 java-devel

r - 在 r 中合并两个图

r - 如何使用 stat ="identity"ggplot 躲避条形图?

r - 在 df2 的日期时间中使用 df1 的 "hour"和 "min"上的条件合并 2 个数据帧

r - 在 R 控制台上查看大数据集

r - 在 R 中选择列的规范方法

r - 从嵌套列表中提取元素到 data.matrix