This is a very simple question, I can’t believe that I can’t understand. I searched high and low for a solution.
I have a named list, for example:
> fitted(mdl)
1 2 3 4 5 6 7 8
-424.8135 -395.0308 -436.5832 -414.3145 -382.9686 -380.7277 -394.2808 -394.3340
9 10 11 12 13 14 15 16
-401.6710 -386.6691 -407.4558 -427.4056 -397.4963 -415.6302 -436.1703 -378.4489
17 18 19 20 21 22 23 24
-353.7718 -377.3190 -390.5177 -370.3608 -389.7843 -397.8872 -401.9937 -390.4119
25 26 27 28 29 30 31 32
-387.4962 -422.4953 -427.1638 -402.5654 -409.6334 -360.7378 -355.1824 -370.9121
33 34 35 36 37 38 39 40
-377.6591 -373.3049 -388.4417 -398.1172 -357.1107 -376.8618 -378.7070 -420.5362
41 42 43 44 45 46 47 48
-390.8324 -406.5956 -403.1015 -363.5008 -347.2580 -371.0433 -376.4454 -360.3895
49
-383.9711
mdlis the object returned from lm()and I'm trying to extract the predicted values using the extracter functionfitted()
I would like it to be without names 1,2,3,.... str()told me that namesis an attribute. I can do
> names(fitted(mdl))
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
[16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
[31] "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "44" "45"
[46] "46" "47" "48" "49"
And this is what I want besides data. After performing various combinations unlist, cbind/rbind, do.call, c()and etc. I finally figured out the solution:
> data.frame(fitted(mdl))$fitted.mdl
[1] -424.8135 -395.0308 -436.5832 -414.3145 -382.9686 -380.7277 -394.2808
[8] -394.3340 -401.6710 -386.6691 -407.4558 -427.4056 -397.4963 -415.6302
[15] -436.1703 -378.4489 -353.7718 -377.3190 -390.5177 -370.3608 -389.7843
[22] -397.8872 -401.9937 -390.4119 -387.4962 -422.4953 -427.1638 -402.5654
[29] -409.6334 -360.7378 -355.1824 -370.9121 -377.6591 -373.3049 -388.4417
[36] -398.1172 -357.1107 -376.8618 -378.7070 -420.5362 -390.8324 -406.5956
[43] -403.1015 -363.5008 -347.2580 -371.0433 -376.4454 -360.3895 -383.9711
But this is a very devious hack for something that should be right under my nose.
Any suggestions on what I am missing?
( , , , , , . :)