运行 列表和 sparkR 中的 which.max 函数
To run the tabulate and which.max function in sparkR
我在 sparkR 中有一个 DataFrame data
。它包含 user = 12 311 12 320, ...
和 type = 1 2 3 4
。我们有 10000 个用户。
例如一位用户的类型 = 1 2 3 4 4 4 2 4。
我想为这个用户找到最常见的整数类型。在 R 中我可以这样解决
mostcommon <- which.max(tabulate(user$type))
鉴于 'user' 是 data.frame 而不是 DataFrame。
我想为 'data' 中的所有用户执行此操作。一种方法是这样
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 中的 运行s 和你为我提供了所有用户最常见的类型。但这需要时间,因为我将数据设为 运行 which.max 和制表函数的本地数据。有没有更聪明、更快捷的方法来做到这一点?
此外,如何找到两种最常见的类型?
也许不是最好的解决方案,但它有效:
创建示例数据
localData <- data.frame(user = c(1,1,1,2,2,2),
type = c(1,2,2,3,3,2))
data <- createDataFrame(sqlContext, localData)
按用户分组并输入并计算它出现的次数(表格形式)
groupedData <- groupBy(data, data$user, data$type)
aggregated <- agg(groupedData, number = n(data$user))
按此计数排序,因为这是查找出现次数最多的类型的最简单方法。
arranged <- arrange(aggregated, desc(aggregated$number))
再次对用户分组并取第一次出现的类型,这是自我们订购以来的最大值。
regroupedData <- groupBy(arranged, arranged$user)
firstItems <- agg(regroupedData, firstType = first(arranged$type), number = first(arranged$number))
查看结果
collect(firstItems)
如果您现在还想要出现次数第二多的项目,您可以先删除这些前几项
firstDeleted <- except(arranged,firstItems)
并再次应用相同的方法
rearranged <- arrange(firstDeleted, desc(firstDeleted$number))
reregroupedData <- groupBy(rearranged, rearranged$user)
secondItems <- agg(reregroupedData, secondType = first(rearranged$type))
删除不需要的列并重命名列
第一个项目 $ 编号 <- NULL
secondItems <- withColumnRenamed(secondItems, "user", "user2")
为了最终的结果,加入这些DataFrames(en delete column user2)
result <- join(firstItems,secondItems, firstItems$user == secondItems$user2)
result$user2 <- NULL
再次检查这些结果
collect(result)
我在 sparkR 中有一个 DataFrame data
。它包含 user = 12 311 12 320, ...
和 type = 1 2 3 4
。我们有 10000 个用户。
例如一位用户的类型 = 1 2 3 4 4 4 2 4。 我想为这个用户找到最常见的整数类型。在 R 中我可以这样解决
mostcommon <- which.max(tabulate(user$type))
鉴于 'user' 是 data.frame 而不是 DataFrame。 我想为 'data' 中的所有用户执行此操作。一种方法是这样
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 中的 运行s 和你为我提供了所有用户最常见的类型。但这需要时间,因为我将数据设为 运行 which.max 和制表函数的本地数据。有没有更聪明、更快捷的方法来做到这一点?
此外,如何找到两种最常见的类型?
也许不是最好的解决方案,但它有效:
创建示例数据
localData <- data.frame(user = c(1,1,1,2,2,2),
type = c(1,2,2,3,3,2))
data <- createDataFrame(sqlContext, localData)
按用户分组并输入并计算它出现的次数(表格形式)
groupedData <- groupBy(data, data$user, data$type)
aggregated <- agg(groupedData, number = n(data$user))
按此计数排序,因为这是查找出现次数最多的类型的最简单方法。
arranged <- arrange(aggregated, desc(aggregated$number))
再次对用户分组并取第一次出现的类型,这是自我们订购以来的最大值。
regroupedData <- groupBy(arranged, arranged$user)
firstItems <- agg(regroupedData, firstType = first(arranged$type), number = first(arranged$number))
查看结果
collect(firstItems)
如果您现在还想要出现次数第二多的项目,您可以先删除这些前几项
firstDeleted <- except(arranged,firstItems)
并再次应用相同的方法
rearranged <- arrange(firstDeleted, desc(firstDeleted$number))
reregroupedData <- groupBy(rearranged, rearranged$user)
secondItems <- agg(reregroupedData, secondType = first(rearranged$type))
删除不需要的列并重命名列 第一个项目 $ 编号 <- NULL secondItems <- withColumnRenamed(secondItems, "user", "user2")
为了最终的结果,加入这些DataFrames(en delete column user2)
result <- join(firstItems,secondItems, firstItems$user == secondItems$user2)
result$user2 <- NULL
再次检查这些结果
collect(result)