我在SparkR上相对较新,并且计划将Spark循环中的for循环转换为foreach循环(R / 3.3.3和Spark / 2.2.0)。
我已经搜索了stackoverflow,唯一相关的线程是: SparkR foreach loop
但是它只能通过使用其他操作来解决。
据我所知,存在一个sparkr
程序包(https://amplab-extras.github.io/SparkR-pkg/rdocs/1.2/index.html)并包含foreach
函数,但是我真的不理解它的用例,我需要一些帮助。 /社区提供帮助的示例。
我的原始R代码示例如下:
uniqueID <- unique(dataset$ID)
maxValueVector <- c()
for(id in uniqueID){
maxValueVector <- c(maximums, max(dataset[which(dataset$ID == id), ]$value))
}
我知道for循环中的行应该分成几行,但是有没有我可以开始使用的示例,例如我可以开始使用的示例foreach
代码?非常感谢!
p.s。 dataset
包含2列:ID
和value
。
答案 0 :(得分:0)
正如评论所说,在SparkR中,我们通常不希望使用foreach。在这种情况下,我使用sparkdataframe
操作符找到了答案,并解决了这个问题:
## IDs is collected fo
IDs <- collect(distinct(select(dataset, 'ID')))
## I added the maximums column in order to figure out the future steps
## it basically satisfied what I need to have.
Maximums <- agg(groupBy(dataset, dataset$ID), maximums = max(dataset$value))
Maximums <- arrange(Maximums, desc(Maximums$maximums))
我知道,因为我仍然对此尚不熟悉,所以此解决方案可能不是您想要的。但是再次感谢您的评论!