[R] Outputing dataframe or vector from within a user defined function

Farrel Buchinsky fjbuch at gmail.com
Wed May 17 21:11:01 CEST 2006


> No, simply access the components you want to get and put then into a
> data.frame.
>
> Let's make a nonsense lm() example that is reproducible for me:
>
>  result <- lapply(1:10, function(i){
>    x <- rnorm(10)
>    y <- rnorm(10)
>    lm(y~x)
>  })
>
> result
>
> Now you want to get the coefficients by applying the indexing function
> "[[" with argument "coefficients" (names of the corresposning element of
> the returned list) to the object result:
>
>  sapply(result, "[[", "coefficients")
>
> (in this particular case one would take the extractor function
> coefficients() instead, but this one was for demonstration purposes).

I have not personally tried it, but there is a wonderful explanation of this 
kind of functionality at
http://wiki.r-project.org/rwiki/doku.php?id=tips:stats-models:extr_regr_sum

It appears to have been added to the Wiki the day we were having this 
discussion.




More information about the R-help mailing list