You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用arules::read.transactions()报错:'cols'与文件表头不匹配(实际匹配)

问题:read.transactions读取事务文件时触发cols不匹配错误

文本文件内容

transactionID item
T100 l1,l2,l3
T200 l2,l4
T300 l2,l3
T400 l1,l2,l3
T500 l1,l3
T600 l2,l3
T700 l1,l3
T800 l1,l2,l3,l5
T900 l1,l2,l3

使用的代码

transacciones <- read.transactions(file = "/home/norhther/Escritorio/trans.txt",
                                   format = "single",
                                   sep = ",",
                                   header = TRUE,
                                   cols = c("transactionID", "item"),
                                   rm.duplicates = TRUE)

错误信息

Error in read.transactions(file = "/home/norhther/Escritorio/trans.txt", :
'cols' does not match entries in header of file.

解决方法

错误核心原因是:文件的列之间用空格分隔,但未指定colSep参数,导致read.transactions无法正确识别表头的两列,进而判定cols参数与表头不匹配。

修正方案1:指定列分隔符为单个空格

修改代码,添加colSep = " "参数,明确列之间的分隔规则:

transacciones <- read.transactions(file = "/home/norhther/Escritorio/trans.txt",
                                   format = "single",
                                   sep = ",",  # 项之间的分隔符(正确保留)
                                   colSep = " ",  # 新增:列之间的分隔符
                                   header = TRUE,
                                   cols = c("transactionID", "item"),
                                   rm.duplicates = TRUE)

修正方案2:兼容多空格/制表符分隔列

如果文件列分隔符是多个空格或制表符,用colSep = "\\s+"匹配任意空白分隔:

transacciones <- read.transactions(file = "/home/norhther/Escritorio/trans.txt",
                                   format = "single",
                                   sep = ",",
                                   colSep = "\\s+",
                                   header = TRUE,
                                   cols = c("transactionID", "item"),
                                   rm.duplicates = TRUE)

额外检查项

  • 确认文件表头无隐藏字符(如制表符、多余空格),确保表头确实是transactionID item,无拼写错误、大小写不一致问题。

内容的提问来源于stack exchange,提问作者Norhther

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:41:29