我有一个 SparkR DataFrame,如下所示:
#Create R data.frame
custId <- c(rep(1001, 5), rep(1002, 3), 1003)
date <- c('2013-08-01','2014-01-01','2014-02-01','2014-03-01','2014-04-01','2014-02-01','2014-03-01','2014-04-01','2014-04-01')
desc <- c('New','New','Good','New', 'Bad','New','Good','Good','New')
newcust <- c(1,1,0,1,0,1,0,0,1)
df <- data.frame(custId, date, desc, newcust)
#Create SparkR DataFrame
df <- createDataFrame(df)
display(df)
custId| date | desc | newcust
--------------------------------------
1001 | 2013-08-01| New | 1
1001 | 2014-01-01| New | 1
1001 | 2014-02-01| Good | 0
1001 | 2014-03-01| New | 1
1001 | 2014-04-01| Bad | 0
1002 | 2014-02-01| New | 1
1002 | 2014-03-01| Good | 0
1002 | 2014-04-01| Good | 0
1003 | 2014-04-01| New | 1
newcust
每次有新客户时都表示有新客户custId
出现,或者如果相同custId
's desc
恢复为“新”。我想要得到的是最后一个desc
每个分组的值newcust
,同时保持第一个date
对于每个分组。下面是我想要获取的DataFrame。我怎样才能在 Spark 中做到这一点? PySpark 或 SparkR 代码都可以工作。
#What I want
custId| date | newcust | finaldesc
----------------------------------------------
1001 | 2013-08-01| 1 | New
1001 | 2014-01-01| 1 | Good
1001 | 2014-03-01| 1 | Bad
1002 | 2014-02-01| 1 | Good
1003 | 2014-04-01| 1 | New