如何解決R語言中 the following objects are masked from data (pos=3)?

我在使用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 cesdreferences 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)可能并不意味著cesdds$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)對方法呢忙菠?

  1. Reference variables directly (e.g. lm(ds$x ~ ds$y))
    直接使用“$”符號(如果變量名較短何鸡,重復(fù)次數(shù)較少時)
  2. Specify the dataframe for commands which support this (e.g. lm(y ~ x, data=ds))
    直接說明命令的數(shù)據(jù)來源,例如: lm(y ~ x, data=ds)
  3. 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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鳄炉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子搜骡,更是在濱河造成了極大的恐慌拂盯,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件记靡,死亡現(xiàn)場離奇詭異谈竿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)摸吠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門空凸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蜕便,你說我怎么就攤上這事》坊茫” “怎么了轿腺?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵两嘴,是天一觀的道長。 經(jīng)常有香客問我族壳,道長憔辫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任仿荆,我火速辦了婚禮贰您,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拢操。我一直安慰自己锦亦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布令境。 她就那樣靜靜地躺著杠园,像睡著了一般。 火紅的嫁衣襯著肌膚如雪舔庶。 梳的紋絲不亂的頭發(fā)上抛蚁,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音惕橙,去河邊找鬼瞧甩。 笑死,一個胖子當(dāng)著我的面吹牛弥鹦,可吹牛的內(nèi)容都是我干的肚逸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼惶凝,長吁一口氣:“原來是場噩夢啊……” “哼吼虎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起苍鲜,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤思灰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后混滔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體洒疚,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年坯屿,在試婚紗的時候發(fā)現(xiàn)自己被綠了油湖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡领跛,死狀恐怖乏德,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤喊括,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布胧瓜,位于F島的核電站,受9級特大地震影響郑什,放射性物質(zhì)發(fā)生泄漏府喳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一蘑拯、第九天 我趴在偏房一處隱蔽的房頂上張望钝满。 院中可真熱鬧,春花似錦申窘、人聲如沸弯蚜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽熟吏。三九已至,卻和暖如春玄窝,著一層夾襖步出監(jiān)牢的瞬間牵寺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工恩脂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留帽氓,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓俩块,卻偏偏與公主長得像黎休,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子玉凯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容