【问题标题】:Importing txt file into R Studio includes unwanted BOM characters "" [duplicate]将 txt 文件导入 R Studio 包含不需要的 BOM 字符“”[重复]
【发布时间】:2016-07-11 17:10:26
【问题描述】:

当我导入以下保存为 UTF-8 编码 Txt 文件的数据时

1   test1
1   test2
2   test1
2   test3

进入 R-Studio 我遇到了 BOM 字符“”出现在结果表中的问题。下面是我用来导入数据的代码。

library(arules)
library(arulesViz)

txn <- read.transactions("r-test.txt",rm.duplicates= FALSE,format="single",sep="\t",cols = c(1,2))
inspect(txn)

生成的导入如下所示:

  items         transactionID
1 {test2}       1            
2 {test1,test3} 2            
3 {test1}       1 

【问题讨论】:

    标签: r text byte-order-mark apriori arules


    【解决方案1】:

    我发现通过将文件保存为 ANSI 编码的 txt 文件可以解决问题。

      items         transactionID
    1 {test1,test2} 1            
    2 {test1,test3} 2  
    

    您可以使用以下 r studio 代码将文件转换为 ANSI 格式:

    writeLines(iconv(readLines("Old File Name"), from = "UTF8", to = "ANSI_X3.4-1986"), 
               file("New File Name", encoding="ANSI_X3.4-1986"))
    

    希望这对遇到同样问题的其他人有所帮助。

    【讨论】:

      【解决方案2】:

      read.transactions 也有一个编码参数。尝试将其设置为“UTF8”

      read.transactions(file, format = c("basket", "single"), sep = "",
                    cols = NULL, rm.duplicates = FALSE, 
                    quote = "\"'", skip = 0, 
                    encoding = "unknown")
      

      【讨论】:

      • 嗨迈克尔,我实际上尝试使用设置为 UTF8 的编码以及带有 BOM 的 UTF8。然而,这两个都没有纠正这个问题。感谢您的回答,如果编码对您有用,请告诉我。
      • 看来我还需要添加编码才能在 read.transactions 中扫描。我将尝试在 github 上的 arules 开发版本中做到这一点。不太确定这是否能解决问题。
      • 谢谢,应该可以完美运行。添加后我会试一试。
      猜你喜欢
      • 2015-11-18
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2017-02-26
      • 2017-09-01
      • 2017-02-09
      • 1970-01-01
      相关资源
      最近更新 更多