[R] simple list question

Ben Bolker bolker at ufl.edu
Sat Dec 13 04:29:56 CET 2008




Barker, Chris wrote:
> 
> 
>   I'd appreciate some tips, I'm  making a simple mistake   trying to
> extract elements from a list of fit objects.
> 
>   The following command works,  coef( cph.list[[1]] )  ,.... coef(
> cph.list[[3]] ) and so forth.  These  extract regression coefficients
> from the appropriate list element. 
> 
>   When I try to run this inside  a for loop, or an lapply
> (lapply(cph.list,coef))  I get this error message
> 
> 
>    > for (ii in (1:22)){
> +  cat(ii,fill=T) 
> +   print(coef( cph.list[[ii]] ))
> + 
> + }
> 1
> Error in object$coefficients : $ operator is invalid for atomic vectors
> 
> 

No obvious mistake/not reproducible: are you sure that all 22 elements
of the list are really linear model fits?  The following made-up example
works just fine for me.
What are the results of sapply(cph.list,class) ?

x <- 1:100
cph.list <- list()
set.seed(1001)
for (ii in 1:22) {
  y <- rnorm(100,1+2*x,sd=1)
  cph.list[[ii]] <- lm(y~x)
}
  
for (ii in 1:22) {
  cat(ii,fill=TRUE)
  print(coef(cph.list[[ii]]))
}

-- 
View this message in context: http://www.nabble.com/simple-list-question-tp20986754p20987041.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list