ivot_longer 具有非常大的 data.frame,内存有效的方法

发布于 2025-01-14 07:59:33 字数 521 浏览 3 评论 0原文

我有一个包含 1100 万行医院数据的 data.frame

Columns: ID (chr), outcome (1|0), 20x ICD-10 codes (chr).
Rows: 10.6 million

我希望使数据整洁,以便将诊断代码建模为二进制结果。

我通常会使用 pivot_longer 或 Base R aggregate 函数,但生成的 data.frame 很大,而且由于内存(32GB RAM、运行最新 R x64 的 Windows 服务器)。

我将分别拆分 data.framepivot_longer 并手动添加列以允许绑定 data.frame 的后面,或者分别对每个拆分的 data.frame 进行建模。

有没有一种方法可以用来减少数据大小或实现我所缺少的类似目标?

I have a data.frame of hospital data with 11 million rows.

Columns: ID (chr), outcome (1|0), 20x ICD-10 codes (chr).
Rows: 10.6 million

I wish to make the data tidy to allow modelling of diagnostic codes to a binary outcome.

I would normally use pivot_longer or Base R aggregate function, but the resulting data.frame is huge and my machine struggles, due to memory (32gb RAM, windows server running latest R x64).

I am going to split the data.frame and pivot_longer for each and manually add columns to allow binding data.frame's after, or to model each split data.frame separately.

Is there a method I could use instead to reduce the data size or achieve a similar objective which I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2025-01-21 07:59:34

尝试使用 data.table::melt 代替:

library(data.table)

DF <- data.frame(ID = LETTERS, replicate(10, sample(0:1, 26, rep=TRUE)))
setDT(DF)
melt(DF, id.vars = "ID")

library(data.table) 提供了基础 R 的 data.frame 的高性能版本(注重速度和内存效率)。

另请参阅此相关基准测试

Try using data.table::melt instead:

library(data.table)

DF <- data.frame(ID = LETTERS, replicate(10, sample(0:1, 26, rep=TRUE)))
setDT(DF)
melt(DF, id.vars = "ID")

library(data.table) provides a high-performance version of base R's data.frame (focus on speed and memory efficiency).

Please also see this related benchmark.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文