我有一个具有这种格式的数据集:
#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"), ",")
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] 删除。
我来说两句