[R] need help with unlist(), losing NULL values

dinesh dinesh.k.somani at gmail.com
Wed Jun 20 07:17:35 CEST 2012


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



More information about the R-help mailing list