If got tons of tibbles with a structure like the following:
library(tidyverse)
library(jsonlite)
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)
UPDATE Following Nad Pads hint, I tried:
data %>% group_by(Type, SubType, SubTypeNr) %>%
summarise(SubTypeVal1 = list(SubTypeVal1),
SubTypeVal2 = list(SubTypeVal2)) %>%
toJSON(pretty = TRUE)
This gets me quite close to what I want (see below). How do I have to group the data to achieve something like this (values nested in SubTypes nested in Types):
{"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],
}
}
}