gpt4 book ai didi

R 'aggregate' 内存不足

转载 作者:行者123 更新时间:2023-12-01 11:42:51 26 4
gpt4 key购买 nike

我有一个关于微博的数据集(600 Mb,包含 5038720 个观察值),我试图计算一个用户在一小时内发布了多少条推文(推文中位数与一条相同)。数据集如下所示:

head(mydata)

uid mid year month date hour min sec
1738914174 3342412291119279 2011 8 3 21 4 12
1738914174 3342413045470746 2011 8 3 21 7 12
1738914174 3342823219232783 2011 8 5 0 17 5
1738914174 3343095924467484 2011 8 5 18 20 43
1738914174 3343131303394795 2011 8 5 20 41 18
1738914174 3343386263030889 2011 8 6 13 34 25

这是我的代码:

count <- function(x) {
length(unique(na.omit(x)))
}
attach(mydata)
hourPost <- aggregate(mid, by=list(uid, hour), FUN=count)

它在那里挂了大约半个小时,我发现所有的真实内存(24 Gb)都被使用了,它开始使用虚拟内存。知道为什么这个小任务会消耗这么多时间和内存吗?我应该如何改进它?提前致谢!

最佳答案

使用包data.table:

mydata <- read.table(text="       uid              mid    year month date hour min sec
1738914174 3342412291119279 2011 8 3 21 4 12
1738914174 3342413045470746 2011 8 3 21 7 12
1738914174 3342823219232783 2011 8 5 0 17 5
1738914174 3343095924467484 2011 8 5 18 20 43
1738914174 3343131303394795 2011 8 5 20 41 18
1738914174 3343386263030889 2011 8 6 13 34 25",
header=TRUE, colClasses = c(rep("character",2),rep("numeric",6)),
stringsAsFactors = FALSE)

library(data.table)
DT <- data.table(mydata)
DT[, length(unique(na.omit(mid))), by=list(uid,hour)]

aggregate 将分组变量强制转换为因子,这可能会占用您的内存(我假设您有多个级别的 uid)。

可能有更多的优化潜力,但您没有提供具有代表性的测试用例。

关于R 'aggregate' 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805060/

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