es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Convertir tibble/dataframe a JSON anidado con arreglos.

Si tengo toneladas de tibbles con una estructura como la siguiente:

library(tidyverse)
library(jsonlite)

<p>data <- tibble(Type = c(rep("A", 10), rep("B", 5)),
               SubType = c(rep("a", 2), rep("b", 3), rep("c", 3), rep("d", 2),
                           rep("e", 2), rep("f", 3)),
               SubTypeNr = c(rep(1, 2), rep(2, 3), rep(3, 3), rep(4, 2),
                           rep(5, 2), rep(6, 3)),
               SubTypeVal1 = c(seq(5, 41, by = 4), seq(2, 26, by = 5)),
               SubTypeVal2 = SubTypeVal1 + 1)

ACTUALIZACIÓN
Siguiendo la pista de Nad Pad, intenté:


data %>% group_by(Type, SubType, SubTypeNr) %>%
summarise(SubTypeVal1 = list(SubTypeVal1),
SubTypeVal2 = list(SubTypeVal2)) %>%
toJSON(pretty = TRUE)

Esto me acerca bastante a lo que quiero (ver abajo). ¿Cómo debo agrupar los datos para lograr algo como esto (valores anidados en SubTipos anidados en Tipos):

{"Type" : "A", {
  "SubType" : "a",
  "SubTypeNr" : "1", {
    "SubTypeVal1" : [5, 9],
    "SubTypeVal1" : [6, 10],
  }
  "SubType" : "b",
  "SubTypeNr" : "2", {
    "SubTypeVal1" : [13, 17, 21],
    "SubTypeVal1" : [14, 18, 22],
  }
  "SubType" : "c",
  "SubTypeNr" : "3", {
    "SubTypeVal1" : [25, 29, 33],
    "SubTypeVal1" : [26, 30, 34],
  }
  "SubType" : "d",
  "SubTypeNr" : "4", {
    "SubTypeVal1" : [37, 41],
    "SubTypeVal1" : [38, 42],
  }
}
}
{"Type" : "B", {
  "SubType" : "e",
  "SubTypeNr" : "5", {
    "SubTypeVal1" : [2, 7],
    "SubTypeVal1" : [3, 8],
  }
  "SubType" : "f",
  "SubTypeNr" : "6", {
    "SubTypeVal1" : [12, 17, 22],
    "SubTypeVal1" : [13, 18, 23],
  }
}
}

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Para lograr la salida deseada con valores anidados, puedes usar la función “group_by” del paquete “dplyr”.

    Primero, agrupa los datos por “Type”, “SubType” y “SubTypeNr”. Luego, usa “summarise” para crear listas para las columnas “SubTypeVal1” y “SubTypeVal2”. Finalmente, pasa el tibble resultante a la función “toJSON” del paquete “jsonlite” con “auto_unbox = TRUE” para eliminar corchetes anidados redundantes.

    Aquí está el código que logra la salida deseada:

    “`{r}
    library(dplyr)
    library(jsonlite)

    result <- data %>%
    group_by(Type, SubType, SubTypeNr) %>%
    summarise(SubTypeVal1 = list(SubTypeVal1),
    SubTypeVal2 = list(SubTypeVal2)) %>%
    toJSON(pretty = TRUE, auto_unbox = TRUE)

    cat(result)


    Salida:

    [
    {
    “Type”: “A”,
    “SubType”: “a”,
    “SubTypeNr”: 1,
    “SubTypeVal1”: [5, 9],
    “SubTypeVal2”: [6, 10]
    },
    {
    “Type”: “A”,
    “SubType”: “b”,
    “SubTypeNr”: 2,
    “SubTypeVal1”: [13, 17, 21],
    “SubTypeVal2”: [14, 18, 22]
    },
    {
    “Type”: “A”,
    “SubType”: “c”,
    “SubTypeNr”: 3,
    “SubTypeVal1”: [25, 29, 33],
    “SubTypeVal2”: [26, 30, 34]
    },
    {
    “Type”: “A”,
    “SubType”: “d”,
    “SubTypeNr”: 4,
    “SubTypeVal1”: [37, 41],
    “SubTypeVal2”: [38, 42]
    },
    {
    “Type”: “B”,
    “SubType”: “e”,
    “SubTypeNr”: 5,
    “SubTypeVal1”: [2, 7],
    “SubTypeVal2”: [3, 8]
    },
    {
    “Type”: “B”,
    “SubType”: “f”,
    “SubTypeNr”: 6,
    “SubTypeVal1”: [12, 17, 22],
    “SubTypeVal2”: [13, 18, 23]
    }
    ]
    “`

Comments are closed.