Flatten a named list in R -
this simple question, can't believe can't figure out. i've searched high , low solution.
i have named list, so:
> 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
mdl
object returned lm()
, , i'm trying extract predicted values using extractor function fitted()
i without 1,2,3,...
names. str()
told me names
attribute. 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 want, except data. after trying various combinations of unlist
,cbind/rbind
, do.call
, c()
, etc. figured out 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 roundabout hack must right under nose.
any suggestions @ i'm missing?
(i don't know how phrase problem well, or come better title question, don't know terminology describe want. feel free edit :)
if you're trying remove names
of object, use unname
.
here's basic example:
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("ctl","trt")) weight <- c(ctl, trt) lm.d9 <- lm(weight ~ group) fitted(lm.d9) # 1 2 3 4 5 6 7 8 9 10 11 12 13 # 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 4.661 4.661 4.661 # 14 15 16 17 18 19 20 # 4.661 4.661 4.661 4.661 4.661 4.661 4.661
remove names:
unname(fitted(lm.d9)) # [1] 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 4.661 4.661 4.661 # [14] 4.661 4.661 4.661 4.661 4.661 4.661 4.661
Comments
Post a Comment