RStudio/Sparklyr 在 MAPR/Spark - 将 , 替换为 .在字符串中

RStudio/Sparklyr on MAPR/Spark - Replace , to . in string

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

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

我喜欢将那些 'strings' 转换为数值。我可以使用 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.

regexp_replace 是您需要的功能:

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

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