本文介绍了提取,格式化和分离已存储在数据框列中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何解析和处理已经存在于数据框中的JSON?
How might I parse and process JSON that already lives inside a data frame?
样本数据:
df <- data.frame(
id = c("x1", "x2"),
y = c('[{"Property":"94","Value":"Error"},{"Property":"C1","Value":"Found Match"},{"Property":"C2","Value":"Address Mismatch"}]', '[{"Property":"81","Value":"XYZ"},{"Property":"D1","Value":"Blah Blah"},{"Property":"Z2","Value":"Email Mismatch"}]')
)
我想提取y
列中的原始JSON并将其格式化并分离为有序的列,最好使用library(jsonlite)
.
I want to extract, format and separate the raw JSON in column y
into orderly columns, ideally with library(jsonlite)
.
提前谢谢!
推荐答案
使用jsonlite
和tidyverse:
Using jsonlite
and the tidyverse:
library(tidyverse)
library(jsonlite)
df %>% mutate(y = map(y, ~fromJSON(as.character(.x)))) %>% unnest()
# Source: local data frame [6 x 3]
#
# id Property Value
# <fctr> <chr> <chr>
# 1 x1 94 Error
# 2 x1 C1 Found Match
# 3 x1 C2 Address Mismatch
# 4 x2 81 XYZ
# 5 x2 D1 Blah Blah
# 6 x2 Z2 Email Mismatch
或不带purrr
df %>% rowwise() %>% mutate(y = list(fromJSON(as.character(y)))) %>% unnest()
,或者仅包含dplyr
和jsonlite
,
df %>% rowwise() %>% do(data.frame(id = .$id, fromJSON(as.character(.$y))))
或仅具有基数R和jsonlite
do.call(rbind,
Map(function(id, y){data.frame(id, fromJSON(as.character(y)))},
df$id, df$y))
所有人都会返回同一件事,所以选择哪个对您来说最有意义.
All return the same thing, so pick which makes the most sense to you.
这篇关于提取,格式化和分离已存储在数据框列中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!