gpt4 book ai didi

r - 如何将数据帧转换为可用格式以在 R 中进行序列挖掘?

转载 作者:行者123 更新时间:2023-12-04 01:57:27 24 4
gpt4 key购买 nike

我想在 R 中进行序列分析,并且我正在尝试将我的数据转换为 arulesSequences 包的可用形式。

library(tidyverse)
library(arules)
library(arulesSequences)

df <- data_frame(personID = c(1, 1, 2, 2, 2),
eventID = c(100, 101, 102, 103, 104),
site = c("google", "facebook", "facebook", "askjeeves", "stackoverflow"),
sequence = c(1, 2, 1, 2, 3))
df.trans <- as(df, "transactions")
transactionInfo(df.trans)$sequenceID <- df$sequence
transactionInfo(df.trans)$eventID <- df$eventID
seq <- cspade(df.trans, parameter = list(support = 0.4), control = list(verbose = TRUE))

如果将我的列保留为上述原始类,则会出现错误:
Error in asMethod(object) : 
column(s) 1, 2, 3, 4 not logical or a factor. Discretize the columns first.

但是,如果我将列转换为因子,则会出现另一个错误:
df <- data_frame(personID = c(1, 1, 2, 2, 2),
eventID = c(100, 101, 102, 103, 104),
site = c("google", "facebook", "facebook", "askjeeves", "stackoverflow"),
sequence = c(1, 2, 1, 2, 3))

df <- as.data.frame(lapply(df, as.factor))
df.trans <- as(df, "transactions")
transactionInfo(df.trans)$sequenceID <- df$sequence
transactionInfo(df.trans)$eventID <- df$eventID
seq <- cspade(df.trans, parameter = list(support = 0.4), control = list(verbose = TRUE))

Error in asMethod(object) :
In makebin(data, file) : 'eventID' is a factor

任何关于解决这个问题的建议或关于 R 中序列挖掘的建议都非常感谢。谢谢!

最佳答案

只有实际项目(在您的情况下为“站点”)进入交易。始终检查您的中间结果以确保它看起来正确。序列挖掘所需的事务类型在 ? cspade 中描述。 .

library("arulesSequences")
df <- data.frame(personID = c(1, 1, 2, 2, 2),
eventID = c(100, 101, 102, 103, 104),
site = c("google", "facebook", "facebook", "askjeeves", "stackoverflow"),
sequence = c(1, 2, 1, 2, 3))

# convert site into itemsets and add sequence and event ids
df.trans <- as(df[,"site", drop = FALSE], "transactions")
transactionInfo(df.trans)$sequenceID <- df$sequence
transactionInfo(df.trans)$eventID <- df$eventID
inspect(df.trans)

# sort by sequenceID
df.trans <- df.trans[order(transactionInfo(df.trans)$sequenceID),]
inspect(df.trans)

# mine sequences
seq <- cspade(df.trans, parameter = list(support = 0.2),
control = list(verbose = TRUE))
inspect(seq)

希望这可以帮助!

关于r - 如何将数据帧转换为可用格式以在 R 中进行序列挖掘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com