gpt4 book ai didi

将一列数据框 reshape 为 R 中的两列

转载 作者:行者123 更新时间:2023-12-01 13:46:23 25 4
gpt4 key购买 nike

假设我有这样一个带有字符的数据框(df)

head
---
a
b
c
d
e
f

我想将 df reshape 为

head1   head2
----- -----
a b
c d
e f

如何在不使用任何循环的情况下进行这样的 reshape 。我会很高兴任何帮助。非常感谢。

最佳答案

我们可以转置 ( t ) 数据集(即 matrix ),然后调用 matrixncol , 并将其重新转换为 data.frame

 as.data.frame(matrix(t(df1), ncol=2, 
byrow=TRUE), stringsAsFactors=FALSE)
# V1 V2
#1 a b
#2 c d
#3 e f

关于将一列数据框 reshape 为 R 中的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com