首页 > 解决方案 > PowerBI中意外的R程序(正则表达式)输出

问题描述

我正在按照本教程将 R 合并到 Microsoft PowerBI,http: //radacad.com/power-bi-and-regular-expressions

使用我自己的数据

我明白如何

# 'dataset' holds the input data for this script
pattern <- "[[:digit:]]{2}[[:alpha:]]{1}[[:digit:]]{3}"
isValid <- function(x) {grepl(pattern , as.character(x), ignore.case=TRUE)}
output <- within(dataset,{Building=isValid(dataset$GROUP_NAME)})

翻译成这个

在此处输入图像描述

但是我如何打印出匹配的字符串本身,即02M477,08X048

我试过了

# 'dataset' holds the input data for this script
pattern <- "[[:digit:]]{2}[[:alpha:]]{1}[[:digit:]]{3}"
isValid <- function(x) {grep(pattern , as.character(x), ignore.case=TRUE, value=TRUE)}
output <- within(dataset,{Building=isValid(dataset$GROUP_NAME)})

我期待

在此处输入图像描述

然而我得到以下错误......如何解决?

DataSource.Error: ADO.NET: R script error.
Error in `[<-.data.frame`(`*tmp*`, nl, value = list(Building = c("Madison\\Manual\\02M477-Shift Stick",  : 
  replacement element 1 has 760 rows, need 768
Calls: within -> within.data.frame -> [<- -> [<-.data.frame
Execution halted

Details:
    DataSourceKind=R
    DataSourcePath=R
    Message=R script error.
Error in `[<-.data.frame`(`*tmp*`, nl, value = list(Building = c("Madison\\Manual\\02M477-Shift Stick",  : 
  replacement element 1 has 760 rows, need 768
Calls: within -> within.data.frame -> [<- -> [<-.data.frame
Execution halted

    ErrorCode=-2147467259
    ExceptionType=Microsoft.PowerBI.Scripting.R.Exceptions.RScriptRuntimeException

标签: rregexpowerbi

解决方案


这应该在 r

regmatches(string, regexpr(pattern, string))

推荐阅读