Arthur Tang
Arthur Tang

Reputation: 1

Error using arules - Error in asMethod(object)

library(arules)
library(rattle)

data <- read.csv('R/347_347.csv', header = TRUE, stringsAsFactors = TRUE)
Data <- data[c(3:23)]
#error in here
trans <- as(Data, "transactions")
Error in asMethod(object) : 
  column(s) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 not logical or a factor. Discretize the columns first.

Upvotes: 0

Views: 5349

Answers (2)

Hack-R
Hack-R

Reputation: 23200

The problem is the data types of your columns.

You need to first convert them to discrete column types like factor (please see https://www.stat.berkeley.edu/classes/s133/factors.html ). Another option is logical, i.e. the boolean values of TRUE or FALSE.

For example:

my_data_frame$x <- as.factor(my_data_frame$x)

You need to do this for each categorical column, such as those mentioned by index number in the error message.

For example:

data(iris)

iris[,1] <- as.factor(iris[,1])
iris[,2] <- as.factor(iris[,2])
iris[,3] <- as.factor(iris[,3])
iris[,4] <- as.factor(iris[,4])
iris[,5] <- as.factor(iris[,5])

iris = as(iris, "transactions")

iris
transactions in sparse format with
 150 transactions (rows) and
 126 items (columns)

Or, even more compactly:

data(iris)
iris = as(data.frame(lapply(iris, as.character), stringsAsFactors=T), "transactions")

Upvotes: 1

SeGa
SeGa

Reputation: 9809

Look at this example which uses some data from the arules package:

library(arules);
data("AdultUCI");
str(AdultUCI)
Adult = as(AdultUCI, "transactions");

AdultUCI <- dplyr::select(AdultUCI, "workclass", "marital-status", "occupation")
str(AdultUCI)

Adult = as(AdultUCI, "transactions");

First, you will see that with the first try to convert it to transactions, the method will fail, giving the same error as yours. But if you select only the factors ("workclass", "marital-status", "occupation"), the method works.

Upvotes: 0

Related Questions