问题描述
我最近从R 3.5.1更新到R 4.0.0. read.csv
的行为似乎已更改-当我在R 4.0.0中加载.csv文件时,不会自动检测到因素,而是将其识别为字符.我还在计算机上运行3.5.1,并且在使用相同代码在3.5.1中加载相同文件时,因素被识别为因素.这有点次优.
I've recently updated to R 4.0.0 from R 3.5.1. The behaviour of read.csv
seems to have changed - when I load .csv files in R 4.0.0 factors are not automatically detected, instead being recognised as characters. I'm also still running 3.5.1 on my machine, and when loading the same files in 3.5.1 using the same code, factors are recognised as factors. This is somewhat suboptimal.
有什么建议吗?
我正在运行Windows 10 Pro,并在Excel 2013中创建.csv文件.
I'm running Windows 10 Pro and create .csv files in Excel 2013.
推荐答案
正如这里的所有人所说-R 4.0.0中的默认行为已更改,并且字符串不再自动转换为因子.这会影响各种功能,包括read.csv()
和data.frame()
.但是,某些明确适用于因素的功能不会受到影响.其中包括expand.grid()
和as.data.frame.table()
.
As everyone here said - the default behaviour have changed in R 4.0.0 and strings aren't automatically converted to factors anymore. This affects various functions, including read.csv()
and data.frame()
. However some functions, that are explicitly made to work with factors, are not affected. These include expand.grid()
and as.data.frame.table()
.
绕过此更改的一种方法是设置全局选项:
One way you can bypass this change is by setting a global option:
options(stringsAsFactors = TRUE)
但这也将被弃用,最终您将不得不手动将字符串转换为因子.
But this will also be deprecated and eventually you will have to convert strings to factors manually.
做出此决定的主要原因似乎是可重复性.自动的字符串到因子的转换会产生因子级别,这些级别可以取决于系统使用的语言环境.因此,如果您来自俄罗斯,并与日本的朋友分享自动转换因子的脚本,那么他可能会得到不同级别的因子水平.
The main reason for such a decision seems to be reproducibility. Automatic string to factor conversion produces factor levels and those levels can depend on the locale used by the system. Hence if you are from Russia and share your script with automatically converted factors with your friend in Japan he might end up with different order of factor levels.
您可以在"The R Blog"上阅读有关此内容的更多信息 stringsAsFactors ,发自 Kurt Hornik
You can read more about this on "The R Blog" stringsAsFactors post by Kurt Hornik
这篇关于read.csv似乎无法检测到R 4.0.0中的因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!