r - Extract items in nested lists -
i have problem: work data frame development time (dependent variable) of 5 species according temperature (independent variable) "by" function calculated lm's of 5 species
by(dados, dados$especie, function(dados) lm(dados$tempo ~ dados$temp, data = dados)
and result got lists nested in other lists yo can see here
list of 5 $ c.albiceps :list of 12 ..$ coefficients : named num [1:2] 262.78 -1.76 .. ..- attr(*, "names")= chr [1:2] "(intercept)" "dados$temp" ..$ residuals : named num [1:41] -4.157 -2.394 -0.631 1.131 2.894 ... .. ..- attr(*, "names")= chr [1:41] "1" "2" "3" "4" ... ..$ effects : named num [1:41] -1344.031 -133.548 0.235 1.977 3.72 ... .. ..- attr(*, "names")= chr [1:41] "(intercept)" "dados$temp" "" "" ... ..$ rank : int 2
it's list of 5 elements (one each specie), , each specie list of 12 elements (from lm function). so, 5 lists of 12, within list of 5.
now, question: want extract values coefficients , sum them up. got list$speciesname$coefficients[2] , want extract each value (the second item of $coefficients, each specie), , want save in vector (in order calculate indices it).
any helpful hints on that?
you can loop on models apply
function , extract coefficients coef
.
## example mods <- by(mtcars, mtcars$cyl, function(x) lm(mpg ~ disp, data=x)) ## sum second coefficients sum(sapply(mods, function(x) coef(x)[[2]]))
Comments
Post a Comment