[R] need help with unlist(), losing NULL values
David Winsemius
dwinsemius at comcast.net
Wed Jun 20 05:50:46 CEST 2012
On Jun 19, 2012, at 6:57 AM, dinesh wrote:
> Hi,
>
> I have a very rudimentary kind of question on using unlist(). I am
> parsing a bunch of JSON text using rjson package. Some data elements
> in a dictionary happen to be null which rjson parses correctly.
>
> > fromJSON(json_str='{"query":{"A":10, "B":null, "C":"hello"},
> "query":{"A":20, "B":null, "C":"hello again"}}')
NULL items are not allowed in atomic vectors and matrices are just
folded vectors. I'm not sure I see a way to turn this into a
dimensioned structure. Conversion of the NULL element to NA might
make more sense.
> c(1,2,NULL,5)
[1] 1 2 5
X <-list("a", NULL, "b")
> unlist(X)
[1] "a" "b"
X[unlist(lapply(X , is.null))] <- NA
> unlist(X)
[1] "a" NA "b"
Trying to replicate list structures from console output is tedious.
Perhaps if you first explain the gals of the effort and then post
dput() applied to this object it might get more prompt attention than
this posting did.
--
David.
> $query
> $query$A
> [1] 10
>
> $query$B
> NULL
>
> $query$C
> [1] "hello"
>
>
> $query
> $query$A
> [1] 20
>
> $query$B
> NULL
>
> $query$C
> [1] "hello again"
>
>
> I need to view this as a matrix, for example as
> matrix(unlist(fromJSON(json_str='{"query":{"A":10, "B":null,
> "C":"hello"}, "query":{"A":20, "B":null, "C":"hello again"}}')), nr=2)
> BUT I lose column "B". It gets worse if some of the B's are not
> null, and if I were really unlucky then matrix() would not fail but
> data will be messed up.
>
> My question is - is there a different way of using unlist() that I
> am missing? Is there a better way of getting this data in a
> rectangular format?
>
> --
> Thanks and regards,
> Dinesh
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list