我在 sparkRdata
中有一个 DataFrame。它包含user = 12 311 12 320, ...
和type = 1 2 3 4
。我们有 10000 个用户。
例如,一个用户的 type = 1 2 3 4 4 4 2 4。我想为这个用户找到 type 中最常见的整数。在 RI 中可以这样解决
mostcommon <- which.max(tabulate(user$type))
鉴于“用户”是一个 data.frame 而不是一个 DataFrame。我想为“数据”中的所有用户执行此操作。一种方法是这样
u<- c()
for(j in 1:10000) {
id <- filter(data, data$user== j)
# For the jth user I make the data local to run the
# which.max and tabulate functions
idlocal <- collect(id)
u[j] <- which.max(tabulate(idlocal$type))
}
这在 R/sparkR 中运行,你为我提供了所有用户最常见的类型。但这需要时间,因为我将数据本地化以运行 which.max 和制表函数。有没有更聪明、更快捷的方法来做到这一点?
此外,如何同时找到两种最常见的类型?