将 txt 文件导入 R Studio 包含不需要的 BOM 字符“”

Importing txt file into R Studio includes unwanted BOM characters ""

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

1   test1
1   test2
2   test1
2   test3

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

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 

我发现通过将文件另存为 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"))

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

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

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