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)
Share:
24,583
Reinaldo Maciel
Author by

Reinaldo Maciel

Updated on January 24, 2020

Comments

  • Reinaldo Maciel
    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
    Roland about 8 years
    The second option is prefered and usually safer.
  • Abel Callejo
    Abel Callejo over 4 years
    This answer doesn't tell about First of all, what is a S4 class?
  • slamballais
    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 than ctree.
  • Gregor Thomas
    Gregor Thomas over 4 years
    ... or even simpler ctree(sex ~ ahe, data = d)?
  • slamballais
    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.)