假设我的数据框如下所示:
我在数据帧的一列中有这个多层 json 字符串response
,我想在同一数据帧中扩展或解析它的多个列。
"[
{\"question\":
{\"id\":314123,
\"text\":\"What did you see in this module?\",
\"question_id\":151,
\"requirements\":
{\"answer_type\":\"text\",
\"text_required\":true}
},
\"response\":
{\"free_text\":
{\"id\":\"314123_ft\",
\"text\":\"Identifying critical knowledge\"}
}
},
{\"question\":
{\"id\":314124,
\"text\":\"What do you feel is the gap?\",
\"question_id\":152,
\"requirements\":
{\"answer_type\":\"text\",
\"text_required\":true}
},
\"response\":
{\"free_text\":
{\"id\":\"314124_ft\",
\"text\":\"Knowledge is clearly mapped out\"}
}
},
{\"question\":
{\"id\":314125,
\"text\":\"Which of the modules you need to focus on\",
\"question_id\":153,
\"requirements\":
{\"answer_type\":\"text\",
\"text_required\":true}
},
\"response\":
{\"free_text\":
{\"id\":\"3141125_ft\",
\"text\":\"preparation of key knowledge\"}
}
}
]"
如果缩进不完全正确,我深表歉意,因为我确实从单行字符串中手动缩进了它。
在此处、此处和此处的其他条目之后,我尝试了几种方法,但它们都无法将整个内容扩展为新列,从而出现我在此处显示的不同错误(我故意只选择代码中行数较少的选项以保持帖子较小)。
> fromJSON(df) %>%
+ unnest(c(response))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘fromJSON’ for signature ‘"data.frame", "missing"’
> df <- df %>%
+ rowwise() %>%
+ do(data.frame(fromJSON(.$response, flatten = T))) %>%
+ ungroup() %>%
+ bind_cols(df %>% select(-response))
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 1, 0
我非常感谢任何 JSON 专家对此的帮助。
这是一种可能的方法,用于
map
将 json 字符串列转换为 dataframe 的列表列,再加上一些额外的处理tidyr
来取消嵌套的 dfs:数据