在csv文件中的市场购物篮分析中查找数据集中的唯一值

时间:2017-01-05 11:10:35

标签: r

我正在进行市场购物篮分析,数据表包含杂货,我想知道有多少独特的商品?请帮帮我

1 个答案:

答案 0 :(得分:0)

考虑例如:

# create demo comma-separated file:
library(arules)
data(Groceries)
lst <- as(Groceries, "list")
writeLines(sapply(lst, paste, collapse=","), tf<-tempfile(fileext = ".csv"))
# readLines(tf)[1:3]
# # [1] "citrus fruit,semi-finished bread,margarine,ready soups"
# # [2] "tropical fruit,yogurt,coffee"                          
# # [3] "whole milk" 

# load csv and check number of items
trans <- read.transactions(tf,"basket",sep=",")
trans
# transactions in sparse format with
#  9835 transactions (rows) and
#  169 items (columns)
ncol(trans)
# [1] 169