使用R从数据集中拆分行在PowerBI上不起作用

伊斯梅尔·瓦莱(Ismael Valle)

我有一个具有这种格式的数据集:

起源

#albaran|fecha|cliente|estado|descrip|destinatario|direccion|cp|poblacion|observaciones
#11111|43229|C1|E1|D1|DD1|DIR1|CP1|P1|COLECCIÓN CLÁSICOS DISNEY - Entrega Nº: 11, 12, 13, 14; Grandes Enigmas - Entrega Nº: 5, 6
#22222|43229|C2|E2|D2|DD2|DIR2|CP2|P2|COLECCIÓN CLÁSICOS DISNEY - Entrega Nº: 8, 9; Otro Pedido - Entrega Nº: 1, 2

我需要转换:

格式

#albaran|fecha|cliente|estado|descrip|destinatario|direccion|cp|poblacion|pedido|entregas
#11111|43229|C1|E1|D1|DD1|DIR1|CP1|P1|COLECCIÓN CLÁSICOS DISNEY|11, 12, 13, 14
#11111|43229|C1|E1|D1|DD1|DIR1|CP1|P1|Grandes Enigmas|5, 6
#22222|43229|C2|E2|D2|DD2|DIR2|CP2|P2|COLECCIÓN CLÁSICOS DISNEY|8, 9
#22222|43229|C2|E2|D2|DD2|DIR2|CP2|P2|Otro Pedido|1, 2

我正在尝试

library(data.table)

hec1 <- as.data.table(dataset)
res <- hec1[,strsplit(observaciones, split = ";"),by = c("albaran", "fecha", "cliente", "estado", "descrip", "destinatario", "direccion", "cp", "poblacion")]
res[, pedido:= substring(observaciones, 1, regexpr(":", observaciones)-2)][, entregas := substring(observaciones, regexpr(":", observaciones)+2, nchar(observaciones))]
res$V1 <- NULL
res <- res[,strsplit(entregas, split = ","),by = c("albaran", "fecha", "cliente", "estado", "descrip", "destinatario", "direccion", "cp", "poblacion", "tipo_pedido")]
setnames(res, "pedido", "entregas")
res

但这不起作用,请向我显示此错误:

strsplit错误(备注,split =“;”):非字符参数调用:[-> [.data.table-> strsplit执行中断

我认为...问题可能是原始格式吗?这是一个data.table

感谢@prem,您的示例在RStudio上运行良好,我试图在PowerBI Script上执行此脚本,但请向我显示此错误

UseMethod(“ separate_rows_”)中的错误:没有适用于'separate_rows_'的适用方法应用于对象... ErrorCode = -2147467259 ExceptionType = Microsoft.PowerBI.Radio.RScriptRuntimeException

当我尝试使用dplyr库时,出现以下错误

DataSource.Error:ADO.NET:R脚本错误。

附件包:“ dplyr”

以下对象被'package:stats'屏蔽:

过滤器,滞后

以下对象从“ package:base”中屏蔽:

相交,setdiff,setequal,联合

UseMethod(“ separate_rows_”)中的错误:没有适用于'separate_rows_'的适用方法应用于类>“ function”的对象调用:%>%... eparate_rows-> sepeparate_rows.default-> eparate_rows_Ejecucióninterrumpida

该软件包与PowerBI https://docs.microsoft.com/es-es/power-bi/service-r-packages-support兼容

这是代码,我不能使用tidyverse,因为它不兼容。

library(ggplot2)
library(tibble)
library(tidyr)
library(readr)
library(ggplot2)
library(stringr)
library(forcats)
library(dplyr)


df %>%
  separate_rows("observaciones", sep = ";") %>%
  separate(observaciones, c("pedido", "entregas"), " - Entrega Nº ")

问题已解决,谢谢@Prem

这是最终版本:

library(ggplot2)
library(tibble)
library(tidyr)
library(readr)
library(ggplot2)
library(stringr)
library(forcats)
library(dplyr)
library(data.table)

df <- as.data.table(dataset)
df <- df %>%
  separate_rows("observaciones", sep = ";") %>%
  separate(observaciones, c("pedido", "entregas"), " - Entrega Nº:")
df <- df %>%
  separate_rows("entregas", sep = ", ") %>%
  separate(entregas, c("entregas"), ",")
df <- df %>%
  separate_rows("entregas", sep = "y") %>%
  separate(entregas, c("entregas"), ",")
Prem

tidyverse 方法可能是

library(tidyverse)

df %>%
  separate_rows("observaciones", sep = ";") %>%
  separate(observaciones, c("pedido", "entregas"), " - Entrega Nº ")

输出为:

  albaran fecha cliente estado descrip destinatario direccion  cp poblacion                    pedido
1   11111 43229      C1     E1      D1          DD1      DIR1 CP1        P1 COLECCIÓN CLÁSICOS DISNEY
2   11111 43229      C1     E1      D1          DD1      DIR1 CP1        P1           Grandes Enigmas
3   22222 43229      C2     E2      D2          DD2      DIR2 CP2        P2 COLECCIÓN CLÁSICOS DISNEY
4   22222 43229      C2     E2      D2          DD2      DIR2 CP2        P2               Otro Pedido
        entregas
1 11, 12, 13, 14
2           5, 6
3           8, 9
4           1, 2

样本数据:

df <- structure(list(albaran = c(11111L, 22222L), fecha = c(43229L, 
43229L), cliente = c("C1", "C2"), estado = c("E1", "E2"), descrip = c("D1", 
"D2"), destinatario = c("DD1", "DD2"), direccion = c("DIR1", 
"DIR2"), cp = c("CP1", "CP2"), poblacion = c("P1", "P2"), observaciones = c("COLECCIÓN CLÁSICOS DISNEY - Entrega Nº 11, 12, 13, 14; Grandes Enigmas - Entrega Nº 5, 6", 
"COLECCIÓN CLÁSICOS DISNEY - Entrega Nº 8, 9; Otro Pedido - Entrega Nº 1, 2"
)), .Names = c("albaran", "fecha", "cliente", "estado", "descrip", 
"destinatario", "direccion", "cp", "poblacion", "observaciones"
), class = "data.frame", row.names = c(NA, -2L))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

setfacl -R在Cygwin上不起作用

ngrok 二进制文件执行在 catalina(mac os) 上不起作用

使用 sed 删除行在 Ubuntu 中不起作用

为什么apply()在R中的数据框上不起作用?

为什么rownames()在R中的数据框上不起作用?

使用REST服务中的数据时,分页在智能表上不起作用

TextWatcher的数据绑定在Android上不起作用

json数据在服务器上不起作用

从剪贴板粘贴数据在 Iphone 上不起作用

Xamarin 表单 HTTPS 数据请求在 android 上不起作用

为什么数据绑定在 UserControl 上不起作用?

PowerBI Azure刷新不起作用,数据未更新

STIRNG_SPLIT 函数在 SSRS 数据集中不起作用

Excel:数据集中为空白时,Sumproduct不起作用

Python pysftp put_r在Windows上不起作用

R:is.integer在平方根上不起作用

数据帧中的R字符串拆分操作不起作用

使用 # 计数实际上不起作用(合金)

使用Javascript加载脚本-在chrome上不起作用?

ProxyPass在使用SSL的Ubuntu上不起作用

使用图标确认消息在postLink上不起作用

Xdebug在使用php7.3的Nginx上不起作用

使用删除按钮删除文件在Fedora上不起作用

Shell脚本在使用Hue的Oozie上不起作用

使用 flexbox 的网站在 IOS 上不起作用

使用bookmarklet注入jQuery在页面上不起作用

SVG 使用 defs 在 chrome 46 上不起作用

.NumberFormat在使用SUMIFS的单元格上不起作用

在PHP中使用substr()在xpath结果上不起作用