attach 資料框後,在 detach 之前都可直接取用資料框中所有變數翻譯
> attach(Duncan)
> prestige
[1] 82 83 90 76 90 87 93 90 52 88 57 89 97 59 73 38 76 81 45 92 39 34 41 16 33 53 67 57 26 29 10 15 19 10 13 24
[37] 20 7 3 16 6 11 8 41 10
> detach(Duncan)
資料框名後接串列取值字元($)加變數名後果以下:
> library(car)
> head(Duncan)
Call:
lm(formula = prestige ~ income + education, data = Duncan)
Coefficients:
(Intercept) income education
-6.0647 0.5987 0.5458
> with(Duncan, {
+ plot(education, prestige)
+ identify(education, prestige, row.names(Duncan))
+ })
參考資料:Fox翻譯社 J. and Weisberg, S. (2011), An R Companion to Applied Regression翻譯社 2nd Edition,Sage.
type income education prestige
accountant prof 62 86 82
pilot prof 72 76 83
architect prof 75 92 90
author prof 55 90 76
chemist prof 64 86 90
minister prof 21 84 87
固然,我們也能夠行使 R 中很多統計建模函數的 data 參數,其效果與前面的 with 指令溝通。
> (lm(prestige ~ income + education, data = Duncan))
> with(Duncan翻譯社 Duncan[type == "bc" & income > mean(income), ])
type income education prestige
electrician bc 47 39 53
RR.engineer bc 81 28 67
plumber bc 44 25 29
streetcar.motorman bc 42 26 19
Call:
lm(formula = prestige ~ income + education)
Coefficients:
(Intercept) income education
-6.0647 0.5987 0.5458
> summary(Duncan)
type income education prestige
bc :21 Min. : 7.00 Min. : 7.00 Min. : 3.00
prof:18 1st Qu.:21.00 1st Qu.: 26.00 1st Qu.:16.00
wc : 6 Median :42.00 Median : 45.00 Median :41.00
Mean :41.87 Mean : 52.56 Mean :47.69
3rd Qu.:64.00 3rd Qu.: 84.00 3rd Qu.:81.00
Max. :81.00 Max. :100.00 Max. :97.00
最後,掏出部分子集與資料繪圖等工作也可搭配 with 指令來利用,此處互動式畫圖的後果並未顯示於圖面上。 以下是一些不贊同 attach 的來由:起首是附加上之資料框變數會遮罩住其它同名的變數;再 [1] 1 3 4 6 10 12 15 20 最近在學會(http://www.r-software.org/)的台灣 R 軟體利用者論壇上有人提起 attach 指令,讓天成翻譯公司想起前一陣子看到的 with 指令翻譯曩昔我們經常使用 attach 指令,將資料框物件附加到搜尋路徑(search path)上,使得資料框中的變數對於 R 的直譯器(interpreter)而言是可見的(visible),省去須敲擊資料框全名方能援用變數的麻煩!以 car 套件中的資料框 Duncan 為例, 撰文者: > Duncan$prestige 本文來自: http://cars2012.pixnet.net/blog/post/21906627-r%e8%bb%9f%e9%ab%94%e4%b8%ad%e7%9a%84attach-%e8%88%87%有關各國語文翻譯公證的問題歡迎諮詢天成翻譯公司02-77260931 |