if null replace with 0, otherwise default value in same column

后端 未结 2 2057
梦如初夏
梦如初夏 2021-01-22 10:23

In SparkR shell 1.5.0, Created a sample data set:

df_test <- createDataFrame(sqlContext, data.frame(mon = c(1,2,3,4,5), year = c(2011,2012,2013,2014,2015)))
d         


        
相关标签:
2条回答
  • 2021-01-22 10:42

    I have used raw SQL case when expression to get the answer,

    df_test3 <- sql(sqlContext, "select mon1, mon, case when year is null then 0 else year end year FROM temp")
    
    showDF(df_test3)
    +----+----+------+
    |mon1| mon|  year|
    +----+----+------+
    | 7.0|null|   0.0|
    | 1.0| 1.0|2011.0|
    | 6.0|null|   0.0|
    | 3.0| 3.0|2013.0|
    | 5.0| 5.0|2015.0|
    | 8.0|null|   0.0|
    | 4.0| 4.0|2014.0|
    | 2.0| 2.0|2012.0|
    +----+----+------+
    

    Even though it gives the answer, i am looking for pure sparkR code.

    0 讨论(0)
  • 2021-01-22 10:59
    df_test2$year <- ifelse(isNull(df_test2$year), 0, df_test2$year)
    

    Pure SparkR code that resolves the problem.

    0 讨论(0)
提交回复
热议问题