我的JSON文件如下所示
[
{
"Hugo_Symbol": "FLT3",
"HGVSp_Short": "p.T227M",
"Other": {
"Type": "Renal",
"Drug": "Sunitinib",
"Effects": "Response"
}
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": {
"Type": "Ovarian",
"Drug": "Cetuximab",
"Effects": "Sensitivity"
}
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": {
"Type": "Lung",
"Drug": "Regorafenib",
"Effects": "Sensitivity"
}
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": {
"Type": "Leukemia",
"Drug": "Akt",
"Effects": "Resistance"
}
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": {
"Type": "Lung",
"Drug": "gefitimab",
"Effects": "Sensitivity"
}
},
{
"Hugo_Symbol": "ALK",
"HGVSp_Short": "p.G845=",
"Other": {
"Type": "",
"Drug": "",
"Effects": ""
}
},
{
"Hugo_Symbol": "APC",
"HGVSp_Short": "p.Q1338*",
"Other": {
"Type": "",
"Drug": "",
"Effects": ""
}
},
{
"Hugo_Symbol": "MET",
"HGVSp_Short": "p. M1S",
"Other": {
"Type": "Eye",
"Drug": "",
"Effects": "Response"
}
}
]我想合并我的JSON文件中的副本
预期产出
[
{
"Hugo_Symbol": "FLT3",
"HGVSp_Short": "p.T227M",
"Other": [
{"Type": "Renal", "Drug": "Sunitinib","Effect": "Response" }
]
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": [
{"Type": "Ovarian","Drug": "Cetuximab","Effect": "Sensitivity"},
{"Type": "Lung","Drug": "Regorafenib","Effect": "Sensitivity"},
{"Type": "Leukemia","Drug": "Akt","Effect": "Resistance"},
{"Type": "Lung", "Drug": "gefitimab", "Effect": "Sensitivity"}
]
},
{
"Hugo_Symbol": "ALK",
"HGVSp_Short": "p.G845=",
"Other": [
{"Type": "", "Drug": "","Effect": "" }
]
},
{
"Hugo_Symbol": "APC",
"HGVSp_Short": "p.Q1338*",
"Other":[
{"Type": "","Drug": "","Effect": ""}
]
},
{
"Hugo_Symbol": "MET",
"HGVSp_Short": "p.M1S",
"OtherTumorInfo": [
{"Type": "Eye","Drug": "","Effect": "Response" }
]
}
]我的初始文件是一个CSV文件,我使用R将我的CSV文件转换成JSON
func <- function(x) {
grps <- split(names(x), gsub("[.].*", "", names(x)))
for (nm in names(grps)) {
if (length(grps[[nm]]) > 1 || !nm %in% grps[[nm]]) {
x[[nm]] <- setNames(subset(x, select = grps[[nm]]),
gsub("^[^.]+[.]", "", grps[[nm]]))
x[,setdiff(grps[[nm]], nm)] <- NULL
}
}
for (nm in names(x)) {
if (is.data.frame(x[[nm]])) {
x[[nm]] <- func(x[[nm]])
}
}
if (any(grepl("[.]", names(x)))) func(x) else x
}我使用这个函数将我的CSV文件转换为嵌套的JSON文件
newdf= func(x)将数据帧转换为JSON
library(jsonlite)
json_f=toJSON(newdf,dataframe = 'rows',pretty = T)我的CSV文件如下
| Hugo_Symbol | HGVSp_Short | Other.Type | Other.Drug | Other.Effects |
| ----------- | ----------- | ---------- | ---------- | ------------- |
|FLT3 | p.T227M | Renal | Sunitinib | Response |
|KRAS | p.G12D | Ovarian | Cetuximab | Sensitivity |
|KRAS | p.G12D | Lung | Regorafenib| Sensitivity |
|KRAS | p.G12D | Leukemia | Akt | Resistance |
|KRAS | p.G12D | Lung | gefitimab | Sensitivity |
|ALK | p.G845= | | | |
|APC | p.Q1338* | | | |
|MET | p. M1S | Eye | | Response |有人能建议我一种简单的方法来合并R中JSON文件中的重复数组吗?
发布于 2022-02-10 12:35:39
在表上进行重新嵌套要容易得多,因此让我们将json转换回tibble:
library(jsonlite)
library(tidyverse)
read_json("data.json") %>%
map(as.data.frame) %>%
bind_rows() %>%
as_tibble() %>%
nest(-Hugo_Symbol, -HGVSp_Short) %>%
mutate(data = data %>% map(~ {
colnames(.x) <- colnames(.x) %>% str_remove("^Other.")
.x
})) %>%
rename(Other = data) %>%
toJSON(pretty = T)结果是
[
{
"Hugo_Symbol": "FLT3",
"HGVSp_Short": "p.T227M",
"Other": [
{
"Type": "Renal",
"Drug": "Sunitinib",
"Effects": "Response"
}
]
},
{
"Hugo_Symbol": "KRAS",
"HGVSp_Short": "p.G12D",
"Other": [
{
"Type": "Ovarian",
"Drug": "Cetuximab",
"Effects": "Sensitivity"
},
{
"Type": "Lung",
"Drug": "Regorafenib",
"Effects": "Sensitivity"
},
{
"Type": "Leukemia",
"Drug": "Akt",
"Effects": "Resistance"
},
{
"Type": "Lung",
"Drug": "gefitimab",
"Effects": "Sensitivity"
}
]
},
{
"Hugo_Symbol": "ALK",
"HGVSp_Short": "p.G845="
...https://stackoverflow.com/questions/71064718
复制相似问题