Reputation: 35
I have a dataset with this format:
#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
And I need to convert on:
#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
I'm trying with
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
But it doesn't work, show me this error:
Error in strsplit(observaciones, split = ";") : argumento de tipo no-carácter Calls: [ -> [.data.table -> strsplit Ejecución interrumpida
I think... can be the problem be the origin format? It's a data.table
.
Thank's @prem, your example work's well on RStudio, im trying to execute this script on PowerBI Script, but show me this error
Error in UseMethod("separate_rows_") : no applicable method for 'separate_rows_' applied to an object ... ErrorCode=-2147467259 ExceptionType=Microsoft.PowerBI.Radio.RScriptRuntimeException
When I try to use dplyr library, I recibe the following error
DataSource.Error: ADO.NET: R script error.
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Error in UseMethod("separate_rows_") : no applicable method for 'separate_rows_' applied to an object of class >"function" Calls: %>% ... separate_rows -> separate_rows.default -> separate_rows_ Ejecución interrumpida
This package is compatible with PowerBI https://learn.microsoft.com/es-es/power-bi/service-r-packages-support
This is the code, i can't use tidyverse because it's not compatible.
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º ")
Problem Solved, thanks @Prem
This is the final version:
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"), ",")
Upvotes: 1
Views: 597
Reputation: 11955
tidyverse
approach could be
library(tidyverse)
df %>%
separate_rows("observaciones", sep = ";") %>%
separate(observaciones, c("pedido", "entregas"), " - Entrega Nº ")
Output is:
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
Sample data:
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))
Upvotes: 0