equivalent of na.locf in sparkR

Viewed 122

I am new to R trying to rewrite an R code in sparkR. One of the operations on data.table named costTbl (which has 5 other columns) is

costTbl[,cost:=na.locf(cost,na.rm=FALSE),by=product_id]
costTbl[,cost:=na.locf(cost,na.rm=FALSE, fromLast=TRUE),by=product_id]

I am unable to find an equivalent operation in sparkR. I thought gapply can be used by grouping the df on product_id and performing this operation. But I am not able to make the code work.

Is gapply the right approach? Is there some other way for achieving this?

2 Answers

Start with some dummy data.

library(SparkR)
library(magrittr)

df <- createDataFrame(data.frame(
  time = c(1, 2, 3, 1, 2, 3),
  product_id = c(1, 1, 1, 2, 2, 2),
  cost = c(1, 2, NA, NA, 2, NA)
))

Use last with na.rm = TRUE and an appropriate window spec.

df %>%
  mutate(
    cost = over(
      last("cost", na.rm = TRUE),
      windowPartitionBy("product_id") %>% orderBy("time") %>% rowsBetween(Window.unboundedPreceding, 0)
    )
  ) %>%
  collect()
#>   time product_id cost locf_cost
#> 1    1          1    1         1
#> 2    2          1    2         2
#> 3    3          1   NA         2
#> 4    1          2   NA        NA
#> 5    2          2    2         2
#> 6    3          2   NA         2
Related