【问题标题】:RStudio/Sparklyr on MAPR/Spark - Replace , to . in stringMAPR/Spark 上的 RStudio/Sparklyr - 替换为 .在字符串中
【发布时间】:2017-02-10 08:49:48
【问题描述】:

我有一个 Spark 数据框 tbl_pred,其中包含以下因子列:

**Value**    
13,3
11
5,3

我喜欢将这些“字符串”转换为数值。我可以使用 as.numeric 函数,但这不起作用,因为我的分隔符是逗号。

tbl_pred <- tbl_bun %>% mutate(value = as.numeric(value))

通常我会使用 sub 函数将 , 替换为 .但此功能不适用于我的 Spark 数据框对象。

Error: org.apache.spark.sql.AnalysisException: Undefined function: 'SUB'. This function is neither a registered temporary function nor a permanent function registered in the database 'xxx'.; line 1 pos 417

有人有将值转换为数字的解决方案吗?

提前致谢,

J.

【问题讨论】:

    标签: r apache-spark sparklyr


    【解决方案1】:

    regexp_replace是你这里需要的函数:

    tbl_bun %>% mutate(value=as.numeric(regexp_replace(value, ",", "\\.")))
    

    如有疑问,请参阅Hive Language Manual UDF。几乎每个函数都具有原生 Spark 实现或作为 Hive UDF 公开。

    【讨论】:

    • 谢谢!正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2018-03-09
    • 2017-03-31
    • 2019-10-23
    • 2023-03-26
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多