Frank Feng wrote: > Dear all, > > > I don't know much about R, but I now have to run R under Python. From > a guy who has much experience with R but not know Python at all, I > get the R statements I need: > >> library("hwde") gdata <- data.frame(Population = c("P","P", "P", >> "N","N","N"), Observed = c(691, 186, 0.5,468, 87, 17 ), locus1 = >> c("AA", "Aa", "aa", "AA", "Aa", "aa")) hwde(data = gdata) > > Run the above in R, then I can get the results I need. > > Python is my programming language, so I installed RPy. I have read > the RPy manual for two days, but I still don't know how to translate > these R statements to Python statements,
There's your mistake! You don't translate the R statements to Python statements, you execute the R statements in R... Suppose you have your data in Python lists called Population, Observed, and locus1: >>> from rpy import r >>> Population = ["P","P", "P", "N","N","N"] >>> Observed = [691, 186, 0.5,468, 87, 17 ] >>> locus1 = ["AA", "Aa", "aa", "AA", "Aa", "aa"] You can transfer data from python variables to R objects with r.assign: >>> r.assign('Observed',Observed) [691.0, 186.0, 0.5, 468.0, 87.0, 17.0] >>> r.assign('Population',Population) ['P', 'P', 'P', 'N', 'N', 'N'] >>> r.assign('locus1',locus1) ['AA', 'Aa', 'aa', 'AA', 'Aa', 'aa'] Now that the data is in R, construct the R data frame object (think of it like a database table) and call hwde: >>> library(hwde) >>> r('gdata=data.frame(Population=Population,Observed=Observed,locus1=locus1)') >>> result = r('hwde(gdata)') [1] "Analysis of Deviance Table" Resid. Df Resid. Dev Df Deviance 1 5 2155.45 +gp 4 2090.58 1 64.87 +a 3 34.85 1 2055.73 +aa 2 34.75 1 0.10 +gp:a 1 34.75 1 4.442e-03 +gp:aa 0 -6.517e-14 1 34.75 There were 12 warnings (use warnings() to see them) Now we have a 'result' variable back in python. This is a dict: >>> result.keys() ['anovatab', 'data.df', 'aovtab.terms'] and somewhere in there will be the numbers you require! Now, the way I've done this avoids most problems with converting between python variables and R objects. Python cannot at the moment store R data frames in python variables and return them safely to R without messing with the Rpy conversion system that seems to confuse newcomers. By mucking about with the conversion settings it's much easier - you can construct the data frame within Python: >>> import rpy >>> rpy.set_default_mode(rpy.NO_CONVERSION) >>> gdata=r.data_frame(Population=Population,Observed=Observed,locus1=locus1) >>> result = r.hwde(gdata) [1] "Analysis of Deviance Table" Resid. Df Resid. Dev Df Deviance 1 5 2155.45 +gp 4 2090.58 1 64.87 +a 3 34.85 1 2055.73 +aa 2 34.75 1 0.10 +gp:a 1 34.75 1 4.442e-03 +gp:aa 0 -6.517e-14 1 34.75 There were 12 warnings (use warnings() to see them) but now 'result' is an unconverted R object: >>> result <Robj object at 0x00A55280> But you can convert this back into a dict by switching back to BASIC_CONVERSION and using the as_py() method: >>> rpy.set_default_mode(rpy.BASIC_CONVERSION) >>> result2 = result.as_py() >>> result2.keys() ['anovatab', 'data.df', 'aovtab.terms'] giving us a Python dict again. I hope this helps - it certainly helps me to refresh myself on all this fiddly stuff once every few weeks by answering this kind of query on the mailing list! Barry ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list