Это сработало для вашего примера:
library(sparklyr)
library(tidyverse)
sc <- spark_connect(master = "local")
df<-tibble(ID=c(1,2,3,4), String=c("a",NaN,"b",NaN))
tbl_df<-sdf_copy_to(sc,df,"tbl_df",overwrite = TRUE)
tbl_df<-tbl_df%>%mutate(String=ifelse(String=="NaN",lag(String,order = ID),String))
> glimpse(tbl_df)
Observations: ??
Variables: 2
$ ID <dbl> 1, 2, 3, 4
$ String <chr> "a", "a", "b", "b"