gpt4 book ai didi

使用 reshape2 反转熔化操作

转载 作者:行者123 更新时间:2023-12-04 19:10:04 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Simpler way to reconstitute a melted data frame back to the original

(1 个回答)


8年前关闭。




考虑以下代码。

library (reshape2)
x = rnorm (20)
y = x + rnorm (rnorm (20, sd = .01))

dfr <- data.frame (x, y)
mlt <- melt (dfr)

当我尝试使用 dcast 反转此操作时,
dcast (mlt, value ~ variable)

我得到了一个包含三列的数据框(例如,不适合散点图)。
如何使用 dcast 重新生成原始数据框?

最佳答案

R 怎么知道在熔化之前存在的顺序?即第一行 x 的概念与 y 的第一行匹配.

如果添加索引列(因为 R 会提示重复的 row.names),您可以简单地执行此操作:

dfr$idx <- seq_along(dfr$x)   
mlt <- melt(dfr, id.var='idx')
dcast(mlt, idx ~ variable, value.var='value')

关于使用 reshape2 反转熔化操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905096/

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