[R] list output

Erin Hodgess erinm.hodgess at gmail.com
Thu Mar 26 20:36:31 CET 2015


It's the double bracket issue.

I replaced that, and all is well.

Thanks to Peter and Don.

Sincerely,
Erin


On Thu, Mar 26, 2015 at 3:21 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:

> I don't think you are correctly keeping track of the structure of your
> objects.
> (and my email software may mess up the indentation, but I can't prevent it)
>
>
> I have not tested, but try replacing
>
>    out[i]$phi_0 <- x$phi_0
>    out[i]$maxlike <- x$maxlike
>    out[i]$bigp <- x$bigp
>
> with
>
>    out[[i]] <- list(phi_0=x$phi_0,
>                     maxlike=x$maxlike,
>                     bigp=x$bigp
>                    )
>
> (double bracket, as Peter suggested)
>
> Then also replace
>   aic[i] <- -2*out[i]$maxlike + (2*(i+1))
> with
>  aic[i] <- -2*out[[i]]$maxlike + (2*(i+1))
>
> To begin understanding this, see this little sequence:
> > foo <- vector('list',3)
> > foo[[2]] <- list(a=1, b=2)
> > foo[2]$a
> NULL
> > foo[[2]]$a
> [1] 1
>
>
>
>
> Also, which() does not return a list object as you are using it, so there
> is no reason to unlist() it. Might as well use
>   which.min(minaic)
> while you're at it.
>
> So replace your last three lines with
>   out[which.min(minaic)]
> as the last line of your function, before the closing }
>
> No need to use return()
>
> -Don
>
>
> --
> Don MacQueen
>
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
>
>
>
>
>
> On 3/26/15, 11:22 AM, "Erin Hodgess" <erinm.hodgess at gmail.com> wrote:
>
> >Hello!
> >
> >I am having some trouble with some list output.
> >
> >Here is my code:
> >geobunch <- function(y) {
> >
> >          out <- vector("list",3)
> >          aic <- numeric(length=3)
> >          print(str(out))
> >          for(i in 1:3) {
> >
> >          x <- geomin(y,i)
> >          print(i)
> >          print(x)
> >          print(str(x))
> >          out[i]$phi_0 <- x$phi_0
> >          out[i]$maxlike <- x$maxlike
> >          out[i]$bigp <- x$bigp
> >          aic[i] <- -2*out[i]$maxlike + (2*(i+1))
> >}
> >          minaic <- which(aic==min(aic))
> >          minout <- out[unlist(minaic)]
> >          return(minout)
> >}
> >
> >And here is the output:
> >> geobunch(ez2a)
> >List of 3
> > $ : NULL
> > $ : NULL
> > $ : NULL
> >NULL
> >[1] 1
> >$phi_0
> >[1] 2.856428
> >
> >$bigp
> >[1] 0.1584016
> >
> >$maxlike
> >[1] -473.0203
> >
> >List of 3
> > $ phi_0  : num 2.86
> > $ bigp   : num 0.158
> > $ maxlike: num -473
> >NULL
> >Error in aic[i] <- -2 * out[i]$maxlike + (2 * (i + 1)) :
> >  replacement has length zero
> >In addition: Warning messages:
> >1: In out[i]$phi_0 <- x$phi_0 :
> >  number of items to replace is not a multiple of replacement length
> >2: In out[i]$maxlike <- x$maxlike :
> >  number of items to replace is not a multiple of replacement length
> >3: In out[i]$bigp <- x$bigp :
> >  number of items to replace is not a multiple of replacement length
> >>
> >
> >I know that the problem is in how I am setting up the "out" variable, but
> >I'm not sure how to correct it.  Also, the bigp variable can take on
> >different lengths.
> >
> >This is on R version 3.1.3, on Ubuntu 14.04.
> >
> >Thank you so much for any help.
> >
> >Sincerely,
> >Erin
> >
> >
> >
> >--
> >Erin Hodgess
> >Associate Professor
> >Department of Mathematical and Statistics
> >University of Houston - Downtown
> >mailto: erinm.hodgess at gmail.com
> >
> >       [[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.
>
>


-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list