首页 > 解决方案 > 如何在 PowerBI 中过滤 R 表达式

问题描述

软件: PowerBI(在https://powerbi.microsoft.com/en-us/downloads/免费下载)

R版本: R-3.5.3

这是我正在关注的过程的动画 Gif

在此处输入图像描述

我正在尝试在 R 代码中进行过滤

这是我的数据

GROUP_NAME    
Madison\Manual\02M477-Shift Stick    
Unknown    
Barbie\Instant\08X048    
Earrings\Instant\30Q739    
Barbie\Instant\08X130    
Earrings\Instant\26Q430

在此处输入图像描述

我希望它看起来像这样

在此处输入图像描述

我尝试添加布尔变量isValid(如果匹配正则表达式则为 TRUE,否则为 FALSE)

然后我尝试isValid在添加到数据集(填充新列 Building)之前进行评估

如果isValid为 TRUE,则输出 digit-digit-letter-digit-digit-digit

否则,如果isValid为 FALSE,则输出“”

# '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)}
bldgCode <- function(x) {grep(pattern , as.character(x), ignore.case=TRUE, value=TRUE)}
output <- within(dataset,{if (isValid==TRUE){Building=bldgCode(dataset$GROUP_NAME)} else " "})

相反,我得到错误,请协助

DataSource.Error: ADO.NET: R script error.
Error in isValid == TRUE : 
  comparison (1) is possible only for atomic and list types
Calls: within -> within.data.frame -> eval -> eval
Execution halted

Details:
    DataSourceKind=R
    DataSourcePath=R
    Message=R script error.
Error in isValid == TRUE : 
  comparison (1) is possible only for atomic and list types
Calls: within -> within.data.frame -> eval -> eval
Execution halted

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

标签: rdataframepowerbi

解决方案


推荐阅读