ANOVA syntax in RPy2
Question: First time using the RPy2 implementation in Python. Attempting to do an one-
way ANOVA with two factors. It works in R on another machine, but Python does
not like the syntax. Any thoughts are appreciated!

    
    
    from rpy2.robjects import aov
    
    huanova = aov(formula = df1['human_den'] ~ df1['region']+df1['years'])
    

Error message points at the tilda.

    
    
    huanova = aov(formula=df1['human_den'] ~ df1['region']+df1['years'])
                                           ^
    SyntaxError: invalid syntax
    


Answer: As per the [documentation about Formulae in
Rpy2](http://rpy.sourceforge.net/rpy2/doc-2.2/html/robjects_formulae.html),
you have to pass the formula as a string. This is one way of doing it:

    
    
    from rpy2.robjects import aov
    from rpy2.robjects import Formula
    
    formula = Formula('human_den ~ region + years')
    env = formula.environment
    env['human_den'] = df1['human_den']
    env['region'] = df1['region']
    env['years'] = df1['years']
    
    huanova = aov(formula = formula)
    

