Error: $ operator not defined for this S4 class
24,583
You are giving as.formula
the wrong input here. Only d$sex ~ d$ahe
should be a formula, so:
ctree(as.formula("d$sex ~ d$ahe"))
Or:
ctree(as.formula("sex ~ ahe"), data = d)
Author by
Reinaldo Maciel
Updated on January 24, 2020Comments
-
Reinaldo Maciel over 4 years
I'm trying to make a formula and I got the error:
$ operator not defined for this S4 class with R.
First of all, what is a S4 class? What am I doing wrong?
Following the code:
as.formula("ctree(d$sex ~ d$ahe , data = d)")
If you want to reproduce it, the dataset (CSV file)
d
is available here. -
Roland about 8 yearsThe second option is prefered and usually safer.
-
Abel Callejo over 4 yearsThis answer doesn't tell about
First of all, what is a S4 class?
-
slamballais over 4 years@AbelCallejo That's because the S4 object has nothing to do with
What am I doing wrong here?
. It's just the error that OP got from misapplying the code, and it could have been a completely different error had OP used a different function thanctree
. -
Gregor Thomas over 4 years... or even simpler
ctree(sex ~ ahe, data = d)
? -
slamballais over 4 years@Gregor-reinstateMonica That could also work, but OP specifically used a string and
as.formula
. (This setup has the unique advantage that you can make strings a priori and then supply them to a function that takes formulas, whereas your answer is hard-coded.)