为什么在R中使用attach()是不可取的,应该怎么用? [英] Why is it not advisable to use attach() in R, and what should I use instead?

查看:175
本文介绍了为什么在R中使用attach()是不可取的,应该怎么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我们有一个数据框架 x ,其中包含列 job 收入。参考框架中的数据通常需要命令 x $ job 用于作业列和<$ 收入列中的数据的c $ c> x $收入



但是,使用命令 attach(x)可以取消数据框的名称,而 $ 符号当提及相同的数据时。因此, x $ job 成为工作 x $收入在R代码中成为收入



问题是,R中的许多专家建议不要使用 attach()在R编码时的命令。



主要原因是什么?应该使用什么?

解决方案

何时使用它:



当我想要在大多数统计数据包(例如Stata,SPSS)中获得的环境中使用一个矩形数据集时,我使用 attach()一次



不使用它时:



然而,它变得非常混乱当您有几个不同的数据集时,代码很快就变得不可读,特别是如果您有效使用R作为粗略的关系数据库,其中不同的数据矩形与所有问题相关,并且可能以各种方式与来自不同的矩形,具有相同名称的变量。



带有()函数的或<$ c $许多函数的c> data = 参数是许多实例的极好选择,其中 attach()是诱人的。


Let's assume that we have a data frame x which contains the columns job and income. Referring to the data in the frame normally requires the commands x$jobfor the data in the job column and x$income for the data in the income column.

However, using the command attach(x) permits to do away with the name of the data frame and the $ symbol when referring to the same data. Consequently, x$job becomes job and x$income becomes income in the R code.

The problem is that many experts in R advise NOT to use the attach() command when coding in R.

What is the main reason for that? What should be used instead?

解决方案

When to use it:

I use attach() when I want the environment you get in most stats packages (eg Stata, SPSS) of working with one rectangular dataset at a time.

When not to use it:

However, it gets very messy and code quickly becomes unreadable when you have several different datasets, particularly if you are in effect using R as a crude relational database, where different rectangles of data, all relevant to the problem at hand and perhaps being used in various ways of matching data from the different rectangles, have variables with the same name.

The with() function, or the data= argument to many functions, are excellent alternatives to many instances where attach() is tempting.

这篇关于为什么在R中使用attach()是不可取的,应该怎么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆