string 如何永久禁用 data.frame 中的 stringsAsFactors=TRUE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8177921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:16:44  来源:igfitidea点击:

How to disable stringsAsFactors=TRUE in data.frame permanently?

stringrdataframe

提问by lokheart

See title. Frankly I am a bit sick of manually doing the adjustment all the time.

见标题。坦率地说,我对一直手动进行调整感到有点厌烦。

This should be a simple question, but I just can't figure out how to fix it. Thanks.

这应该是一个简单的问题,但我不知道如何解决它。谢谢。

回答by joran

Set options(stringsAsFactors = FALSE)at the beginning of your R session, or in your .RProfile.

设置options(stringsAsFactors = FALSE)你的R对话的开始,或在您的.RProfile。

As the comments below may suggest, stringsAsFactorsis a bit of a controversial topic within the R community. How irritating you find this default value may depend somewhat on how much time you spend using R to fit many "standard" statistical models (lm, glm, etc). Many of those model fitting and related functions are built around using the factor data type.

正如下面的评论可能暗示的那样,这stringsAsFactors是 R 社区中一个有争议的话题。如何刺激你觉得这个默认值可能在你花了多少时间,使用R,以适应许多“标准”统计模型(有点依赖lmglm等等)。许多模型拟合和相关函数都是围绕使用因子数据类型构建的。

If you spend most of your time doing other more "generic" types of data analysis, you might find this default more irritating.

如果您将大部分时间花在其他更“通用”类型的数据分析上,您可能会发现这种默认设置更令人恼火。

It is widely considered dangerous to globally set stringsAsFactors = FALSEfor the reasons mentioned below: it can cause significant confusion when sharing code. Indeed, even if you work mainly alone, participating in online communities like StackOverflow can be tricky if you insist on running R with stringsAsFactors = FALSE: your answer to a question may not work for the OP, or you may not be able to replicate errors others are seeing!

stringsAsFactors = FALSE由于下面提到的原因,全局设置被广泛认为是危险的:共享代码时可能会导致严重的混乱。实际上,即使您主要是独自工作,如果您坚持使用 R 运行 R,参与 StackOverflow 等在线社区也可能会很棘手stringsAsFactors = FALSE:您对问题的回答可能不适用于 OP,或者您可能无法复制其他人看到的错误!

Of course, everyone can make their own choices about how best to manage these risks for themselves.

当然,每个人都可以自己选择如何最好地管理这些风险。