Quantcast

need help with unlist(), losing NULL values

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

need help with unlist(), losing NULL values

dinesh-2
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"}}')
$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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: need help with unlist(), losing NULL values

David Winsemius

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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: need help with unlist(), losing NULL values

dinesh-2
Hi David,

Thanks for clarifying and the hints. Now that I know, I have already
rewritten it differently and that works. At its core it was quite simply:

 > foo <- fromJSON(json_str='{"query":{"A":10, "B":null, "C":"hello"},
"query":{"A":20, "B":null, "C":"hello again"}}')
 > as.matrix(t(sapply(foo, function(s) s)))
       A  B    C
query 10 NULL "hello"
query 20 NULL "hello again"

Regards
Dinesh


On 6/19/2012 8:50 PM, David Winsemius wrote:

>
> 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.
>


--
Regards,
Dinesh

______________________________________________
[hidden email] 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.
Loading...