I have a data table where one of the columns contains JSON. I am trying to extract the contents so that each variable is a column.
library(jsonlite)
library(data.table)
df<-data.table(a=c('{"tag_id":"34","response_id":2}',
'{"tag_id":"4","response_id":1,"other":4}',
'{"tag_id":"34"}'),stringsAsFactors=F)
The desired result, which does not apply to the "other" variable:
tag_id response_id
1 "34" 2
2 "4" 1
3 "34" NA
I tried several versions:
parseLog <- function(x){
if (is.na(x))
e=c(tag_id=NA,response_id=NA)
else{
j=fromJSON(x)
e=c(tag_id=as.integer(j$tag_id),response_id=j$response_id)
}
e
}
which seems to work well to get a list of vectors (or lists, if c is replaced by a list), but when I try to convert the list to data.table, something doesn't work properly.
parsed<-lapply(df$a,parseLog)
rparsed<-do.call(rbind.data.frame,parsed)
colnames(rparsed)<-c("tag_id","response_id")
Due to a missing value in the third row. How can I solve this problem in pure R-ish? How can I make the parse method return NA for a missing value. Alternative. Is there a "fill" parameter, for example, for rbind, which can be used in rbind.data.frame or a similar method?
, , 11M , .
, rbind.data.frame . ? , rbindlist, , , ( do.call ):
rparsed<-do.call(rbindlist,fill=T,parsed)
: , , , 11M :
df<-data.table(a=c('{"tag_id":"34","response_id":2}',
'{"trash":"34","useless":2}',
'{"tag_id":"4","response_id":1,"other":4}',
NA,
'{"response_id":"34"}',
'{"tag_id":"34"}'),stringsAsFactors=F)
tag_id response_id.