Extract, format and separate JSON already stored in a data frame column

▼魔方 西西 提交于 2019-11-30 10:54:13

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

or without purrr,

df %>% rowwise() %>% mutate(y = list(fromJSON(as.character(y)))) %>% unnest()

or with just dplyr and jsonlite,

df %>% rowwise() %>% do(data.frame(id = .$id, fromJSON(as.character(.$y))))

or with just base R and 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!