我在使用R語言的時候半沽,經(jīng)常會遇到“ the following objects are masked from data (pos=3)”這樣的提示母截,因?yàn)槊看味寄苓\(yùn)行出結(jié)果到忽,所以每次也都沒管它。直到我今天跑循環(huán)的時候清寇,這個東西真的讓我好煩喘漏,就想搞清楚怎么回事,再消除這樣的提示华烟。百度了許久翩迈,網(wǎng)上并沒有合適的答案,于是去了外網(wǎng)搜索盔夜,結(jié)果如下:
標(biāo)題很是有意思哈:
還是看正文吧:
R objects that reside in other R objects can require a lot of typing to access. For example, to refer to a variable x
in a dataframe df
, one could type df$x
. This is no problem when the dataframe and variable names are short, but can become burdensome when longer names or repeated references are required, or objects in complicated structures must be accessed.
大概翻譯:實(shí)踐中负饲,我們想使用數(shù)據(jù)框
df
中的x
通常會用命令df$x
即可,但是當(dāng)名字較長或者要重復(fù)很多次的時候就很繁瑣了喂链。
The attach()
function in R can be used to make objects within dataframes accessible in R with fewer keystrokes. As an example:
在這種情況下返十,
attach()
命令則會讓我們輕松許多,即減少工作量椭微。例如:
ds <- read.csv("http://www.math.smith.edu/r/data/help.csv")#讀取數(shù)據(jù)
names(ds)
attach(ds) #加載ds數(shù)據(jù)
mean(cesd)
[1] 32.84768 #結(jié)果
... then detach()
the dataset to clean up after ourselves.
之后洞坑,用命令detach()
結(jié)束使用數(shù)據(jù)集。
users are cautioned that if there is already a variable called cesd
in the local workspace, issuing attach(ds)
, may not mean that cesd
references ds$cesd
. Name conflicts of this type are a common problem with attach()
and care should be taken to avoid them.
- 用戶應(yīng)注意蝇率,如果本地工作空間中已存在名為
cesd
的變量迟杂,則使用attach(ds)
可能并不意味著cesd
是ds$cesd
之意刽沾。這種類型的名稱沖突是attach()的常見問題
,應(yīng)注意避免它們排拷。
The help page for attach()
notes that attach can lead to confusion. The Google R Style Manual provides clear advice on this point, providing the following advice about attach()
: The possibilities for creating errors when using attach are numerous. Avoid it.
attach()
的help頁面提示說“使用attach命令可能會導(dǎo)致混淆”侧漓。關(guān)于這一點(diǎn),Google R Style Manual給出了明確的建議:使用attach()
導(dǎo)致的錯誤可能有很多攻泼,避免使用它火架!
So what options exist for those who decide to go cold turkey?
那么,有哪些應(yīng)對方法呢忙菠?
- Reference variables directly (e.g.
lm(ds$x ~ ds$y)
)
直接使用“$”符號(如果變量名較短何鸡,重復(fù)次數(shù)較少時) - Specify the dataframe for commands which support this (e.g.
lm(y ~ x, data=ds)
)
直接說明命令的數(shù)據(jù)來源,例如:lm(y ~ x, data=ds)
- Use the
with()
function, which returns the value of whatever expression is evaluated (e.g.with(ds,lm(y ~x))
)
使用with()
函數(shù)牛欢,它會返回被評估的任何表達(dá)式的值骡男,例如with(ds,lm(y~x))
(Also note the within()
function, which is similar to with()
, but returns a modified object.)
注:
within()
函數(shù)和with()
函數(shù)是近似的傍睹。
Some examples may be helpful:
比如下面這個例子:
直接用$符號:
> lm1 <- lm(cesd ~ pcs, data=ds)
> mean(ds$cesd[ds$female==1]) # these next three are equivalent
[1] 36.88785
或者用with命令:
> with(ds, mean(cesd[female==1]))
[1] 36.88785
> with(subset(ds, female==1), mean(cesd))
[1] 36.88785
In short, there’s never an actual need to use attach()
, using it can lead to confusion or errors, and alternatives exists that avoid the problems. We recommend against it.
- 簡而言之隔盛,我們沒有必要使用attach(),因?yàn)樗鼤?dǎo)致混淆或錯誤拾稳,何況我們有其它替代方案吮炕。
- 故,我們建議不使用它访得!
結(jié)論:盡量不用 attach 命令龙亲!
- 替代它的 with 命令怎么用?
with(data,{...})
詳細(xì)語法可以在R中輸入??with
查看悍抑。
文獻(xiàn)參考:https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/