【问题标题】:read.csv replaces column-name characters like `?` with `.`, `-` with `...`read.csv 用 `.` 替换列名字符,例如 `?`,用 `...` 替换 `-`
【发布时间】:2018-05-19 10:12:48
【问题描述】:

我正在使用 RStudio,我在控制台上的输出被截断。我找不到如何停止截断(我尝试搜索 ?options 以及在谷歌上搜索的时间比我想承认的要长)。

编辑:我向大家道歉!我最初的长名称为“ThisIsAReallyReallyReallyReallyReallyLongName”,但问题只是出现了“翻译服务信息 - 哪个翻译服务?”的长名称。我想我找到了问题所在。 ... 没有截断,它用.... 替换了?- 等未知字符。

代码

# Load File
myfile <- read.csv(file="C:\\Users\\wliu\\Desktop\\myfile.csv",
               sep=",", header=TRUE, stringsAsFactors=FALSE, skip=2)

# Get my column names
mycolnames <- colnames(myfile)

# When I request a shorter name, this returns the full name
mycolnames[1]  # Assuming first col is a short name
[1] "ThisIsAShortName"

# However, when I request a longer name, this returns a truncated version
mycolnames[2]  # Assuming second col is a really long name
[1] "ThisIsA...Long...Name"

我想取回mycolnames[2] 的非截断版本(例如“ThisIsAReallyReallyReallyReallyReallyLongName”)

设置

我使用的是 Windows 7 64 位,RStudio 版本 0.98.1091,R 版本 3.0.1 (2013-05-16) -- “Good Sport”,平台:x86_64-w64-mingw32/x64(64 位) .我尝试打开和关闭“使用 Git Bash 作为 Git 项目的 shell”。

myfile.csv

ThisIsAShortName, Translation Service Info - Which translation service?

23143505, Yes
23143614, No
23143324, Yes

【问题讨论】:

  • 嗯,我没有得到相同的结果。例如names(data.frame(ThisIsAReallyReallyReallyReallyReallyLongName=1:10)) 返回未截断的名称
  • @Will 虽然,我无法测试它(因为我没有 Rstudio),你使用的是最新版本的 Rstudio。
  • 我无法在 Mac 版本 0.98.1091 上重现
  • 我也无法重现这个。您能否包含实际存储在文件中的列名(例如,myfile.csv 的一小段摘录)?
  • 名称是否肯定被截断 - 即在读入数据时名称中是否有特殊字符被转换为点?

标签: r rstudio names read.csv


【解决方案1】:

这是read.csv 的预期行为,而不是 R 中的截断问题。当文件的列名中有空格和特殊字符时,read.csv 会用. 替换它们中的每一个,除非你指定@ 987654324@

这里是make.names 的一瞥,这是read.table 生成列名的方式。

nm <- "Translation Service Info - Which translation service?"
make.names(nm)
# [1] "Translation.Service.Info...Which.translation.service."

这是来自read.table的相关行

if (check.names) 
        col.names <- make.names(col.names, unique = TRUE)

【讨论】:

    【解决方案2】:

    在 RStudio 中,使用菜单:

    Tools &gt; Global Options &gt; Code &gt; Display

    在“控制台”部分,将Limit length of lines displayed in console to: 设置为比当前设置大得多的某个数字。

    点击确定。

    【讨论】:

      【解决方案3】:

      怎么样:

      options(width=300)
      

      这能解决问题吗?

      【讨论】:

      • 问题最终是我的特定文件的特殊字符。我举了一个不好的例子(对不起),当我意识到我的错误时我后来编辑了它。我会对此表示赞同,因为它可以解决普通文件的截断问题。
      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多