【问题标题】:Concatenate Rows from dataset on R, doing group by another column从 R 上的数据集中连接行,按另一列分组
【发布时间】:2018-09-27 16:12:55
【问题描述】:

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

我需要转换:

我可以不用循环吗?

编辑:

嗨,再次感谢@guscht,我正在尝试使用你的示例,但我遇到了一些问题,我需要在 PowerBI 上使用它和我的数据集。

我正在尝试

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[, tipo_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, "tipo_pedido", "entregas")
res

但它不起作用,告诉我这个错误:

Error in strsplit(observaciones, split = ";") : 
  argumento de tipo no-carácter
Calls: [ -> [.data.table -> strsplit
Ejecución interrumpida

我认为...可能是原始格式的问题?它是一个数据表

【问题讨论】:

    标签: r loops dataset


    【解决方案1】:

    使用data.table-package 您可以执行以下操作:

    dt <- fread(input = '
    16/04/2018 23:18|Estrella Disney|1|sandy crespo
    16/04/2018 23:18|Estrella Disney|2|sandy crespo
    16/04/2018 23:18|Estrella Disney|3|sandy crespo
    16/04/2018 23:18|Estrella Disney|4|sandy crespo
    16/04/2018 23:18|Estrella Disney|5|sandy crespo
    16/04/2018 23:18|Estrella Disney|6|sandy crespo
    16/04/2018 23:18|Colleccion|20|sandy crespo
    16/04/2018 23:18|Colleccion|4|sandy crespo
    ', sep = '|')
    setnames(dt, c('date_time', 'something', 'number', 'user'))
    
    res <- dt[, paste(number, collapse = ", "), by = c("something", "user", "date_time")][, paste(something, ":", V1, collapse = "; "), by = c("user", "date_time")]
    res <- res[, c('date_time', 'V1', 'user'), with = F]
    res
              date_time                                                 V1         user
    1: 16/04/2018 23:18 Estrella Disney : 1, 2, 3, 4, 5, 6; Colleccion : 20, 4 sandy crespo
    

    本质上,这种方法使用两个collapse-statements 来生成您想要的列。第一个生成连接数,第二个生成具有连接数的变量。 by-statements 只是指定不修改和保留哪些列。

    编辑:我更改了上面的代码,在something 和数字之间添加了一个:。 要反转该过程,您可以执行以下操作:

    res <- res[,strsplit(V1, split = ";"),by = c("user","date_time")]
    res[, something := substring(V1, 1, regexpr(":", V1)-2)][, number := substring(V1, regexpr(":", V1)+2, nchar(V1))]
    res$V1 <- NULL
    res <- res[,strsplit(number, split = ","),by = c("user","date_time","something")]
    setnames(res, "V1", "number")
    res
               user        date_time       something number
    1: sandy crespo 16/04/2018 23:18 Estrella Disney      1
    2: sandy crespo 16/04/2018 23:18 Estrella Disney      2
    3: sandy crespo 16/04/2018 23:18 Estrella Disney      3
    4: sandy crespo 16/04/2018 23:18 Estrella Disney      4
    5: sandy crespo 16/04/2018 23:18 Estrella Disney      5
    6: sandy crespo 16/04/2018 23:18 Estrella Disney      6
    7: sandy crespo 16/04/2018 23:18      Colleccion     20
    8: sandy crespo 16/04/2018 23:18      Colleccion      4
    

    【讨论】:

    • 哇,非常感谢,它对我非常有用,我是 R 的菜鸟...如果我需要做相反的过程,¿它可能像分裂一样吗?再次感谢您的宝贵时间,问候
    • @IsmaelValle 调整了上面的答案。
    • 嗨,再次感谢,我正在尝试使用你的示例,但我遇到了一些问题,我需要在 PowerBI 上使用它和我的数据集。
    • @IsmaelValle,我不知道 PowerBI。请为此打开一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 2017-01-23
    • 2021-05-16
    • 2019-10-16
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多