newbie help -- $ operator is invalid for atomic vectors

I'm brand new to R. I'm trying to run some sample code and am getting the following Error.

Error in labels(lhs(rulesMatchLHS))elements : ** ** operator is invalid for atomic vectors

Below is my code:


ItemsBasket = read.csv("ItemsBasket.csv")
dataset1 = read.csv("R Bike Input.csv")

library (arules)
library(arulesViz)

tr <-read.transactions("ItemsBasket.csv", format="basket", sep = "," )
warnings()
rules <- apriori(tr, parameter = list(supp = 0.001, conf = 0.8))
rules <- sort(rules, by="confidence", decreasing=TRUE)

dataset1 <- as.vector(t(dataset1))
rulesMatchLHS <- subset(rules,lhs %ain% dataset1)
OutputClient =data.frame(  lhs = labels(lhs(rulesMatchLHS))$elements,
                           rhs = labels(rhs(rulesMatchLHS))$elements,
                           rulesMatchLHS@quality)

The code is from here:

THANKS!!

2 Likes

I do not have any experience with the packages you are using, so I can only give you some general advice. The error is happening at

labels(lhs(rulesMatchLHS))$elements

and the complaint is that the $ operator is not valid for atomic vectors. That tells us that

labels(lhs(rulesMatchLHS))

is not returning a list as expected by the code. I would first run

tmp <- labels(lhs(rulesMatchLHS))

and inspect tmp with the str() and class() functions

str(tmp)
class(tmp)

I would then inspect rulesMatchLHS with the same functions. It should have the class rules. Maybe that will allow you to figure out what is wrong or you can report the results of those efforts here and someone might be able to provide more guidance.

I also am not familiar with these packages at all, but I wanted to chime in and offer both condolences and encouragement, as this is an error that I regularly find myself hitting and which you will probably see again. I think you are in good company, though, and not just of people new to R :slight_smile:

Basically, somewhere a $ is getting fired on something or in a way that it shouldn't be. I usually end up debugging it by digging in more intentionally into the objects I am working with, simplifying my example, or breaking out "one liners" into more complete chunks.

i.e. in your case, I would take the rulesMatchLHS object, play with it, try t1 <- lhs(rulesMatchLHS), and maybe t2 <- labels(t1). Eventually, I'd get around to the $ and try to understand what is breaking where.

Not sure if it helps, but definitely don't give up! If I had a nickel for every time I got through this error... I'd have a lot of nickels. I think I saw it yesterday... You will do it!

(Aside: your github repo may make this possible, but digging into the skills of creating a reprex is probably one of the single most valuable skills you can develop as a learner! It helps you not only isolate your problem, but ask better questions that others can dig into. It is particularly helpful when you run into a bug in a package that you do not maintain or any hard-to-explain behavior)

https://www.jessemaegan.com/post/so-you-ve-been-asked-to-make-a-reprex/

3 Likes

Thank you all for the suggestions! I think I created the reprex it is below.

Rendering reprex...
Error in parse(text = x, keep.source = TRUE) : 
  <text>:30:0: unexpected end of input
28: 
29:

Links to the data files
https://raw.githubusercontent.com/oliviak/Recommender-in-Azure/master/3%20Collaborative%20Filtering/ItemsBasket.csv

https://raw.githubusercontent.com/oliviak/Recommender-in-Azure/master/3%20Collaborative%20Filtering/R%20Bike%20Input.csv

I think that R Bike Input.csv and ItemsBasket.csv are interchanged in the code. Looking through https://github.com/oliviak/Recommender-in-Azure/tree/master/3%20Collaborative%20Filtering, the variable tr is supposed to contain 20745 transactions. Those can only come from R Bike Input.csv. Similarly, dataset1 is supposed to be a 1 x 2 matrix and that has to be ItemsBasket.csv. Try this version of the code. Note that I dropped the $element in the construction of the OutputClient data frame. The code still throws warnings, put it does produce a data frame that looks plausible to me.

#ItemsBasket = read.csv("ItemsBasket.csv")
dataset1 = read.csv("ItemsBasket.csv") #R Bike Input.csv

library (arules)
#library(arulesViz)

tr <-read.transactions("R Bike Input.csv", format="basket", sep = "," )
#warnings()
rules <- apriori(tr, parameter = list(supp = 0.001, conf = 0.8))
rules <- sort(rules, by="confidence", decreasing=TRUE)

dataset1 <- as.vector(t(dataset1))
rulesMatchLHS <- subset(rules,lhs %ain% dataset1)
OutputClient =data.frame(  lhs = labels(lhs(rulesMatchLHS)), #$elements
                           rhs = labels(rhs(rulesMatchLHS)),
                           rulesMatchLHS@quality)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.