[R] check a list that a sublist exists

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Tue Mar 3 11:44:25 CET 2015


Hello, Alaios,

try

is.null( Mylist[[i]]$parameters)

It returns TRUE if your list doesn't have a component named "parameters" 
or if that component contains NULL.

  HtH  --  Gerrit


On Tue, 3 Mar 2015, Alaios via R-help wrote:

> Hi all,I have a list that has the following fields.
> $`80`
> [1] "Error in if (fitcass1[[2]] == \"Error\") { : \n  Fehlender Wert, wo TRUE/FALSE nötig ist\n"
> attr(,"class")
> [1] "try-error"
> attr(,"condition")
> <simpleError in if (fitcass1[[2]] == "Error") {    print(sprintf("error at fitting gamma distribution with %s periods. Mean %f %f Sd %f %f",         flag, mean1, mean2, sd1, sd2))} else {    return(fitcass1)}: Fehlender Wert, wo TRUE/FALSE nötig ist>
>
>
> $`81`
> [1] 0
>
> $`9`
> [1] 0
>
> $`79`
> $parameters
>             pi           mu    sigma
> 1 0.9996796725     1.654832 127.6542
> 2 0.0003203275 17183.001125 302.8063
>
> $se
>          pi.se      mu.se sigma.se
> 1 2.113882e-05  0.1439152 14.22274
> 2 2.113882e-05 38.3582148      NaN
>
> $distribution
> [1] "gamma"
>
>
>
> and so one. The content of each first level sublist are never the same. I want for each first-level sublist my list has to check fast that the current element , lets say the 79th has the $parameters.then I would keep only the numbers from the sublists that have this $parameters inside them and skip all the rest.
> I tried something like exists(Mylist[[i]]$parameters) but it does not workI also tried the is.numeric(Mylist[[i]]$parameters)) but this line fails when the current sublist does not contain the $parameters field.
> Can you please help me sort this out?
> RegardsAlex
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


More information about the R-help mailing list