我有一个包含json数据的data.frame:
dat1 = data.frame(id = 001, name = "Mac", score = '{"math":5}', date = '{"date1":"2018-01-19","date2":"2018-01-20"}')
dat2 = data.frame(id = 002, name = "Eric", score = '{"math":7}', date = '{"date1":"2018-01-18","date2":"2018-01-20"}')
dat = rbind(dat1, dat2)
我想获得以下data.frame:
# id name score.math date.date1 date.date2
# 1 1 Mac 5 2018-01-19 2018-01-20
# 2 2 Eric 7 2018-01-18 2018-01-20
我想首先使用jsonlite :: fromJSON然后使用rbind数据。
答案 0 :(得分:0)
以下是jsonlite
library(jsonlite)
data.frame(c(dat[1:2], unlist(lapply(dat[3:4], function(x)
fromJSON(paste0("[", paste(x, collapse=",\n"), "]"))),
recursive = FALSE)), stringsAsFactors = FALSE)
# id name score.math date.date1 date.date2
#1 1 Mac 5 2018-01-19 2018-01-20
#2 2 Eric 7 2018-01-18 2018-01-20