天天看點

R語言筆記 attach()、detach()和with()

函數attach()可将資料框添加到R的搜尋路徑中。R在遇到一個變量名以後,将檢查搜尋路

徑中的資料框,以定位到這個變量

 summary(mtcars)

> plot(mtcars$mpg)

> plot(mtcars$hp)

mtcars 裡的字段盡量是變量中沒有的

> attach(mtcars)

> plot(mpg)

> plot(hp)

> detach(mtcars)

函數detach()将資料框從搜尋路徑中移除。值得注意的是,detach()并不會對資料框本身

做任何處理

除此之外,另一種方式是使用函數with()。你可以這樣重寫上例:

> with(mtcars,{

繼續閱讀