Albert Meroño Peñuela wrote: > Hello everyone, > > I'm writing a little script in Python which intends to sort a table of > word frequency in a text, using R functions from rpy package. I've got > my word list; when I use the R table function I obtain a list which > contains que frequencies, but *not* the word names: > > #!/usr/bin/python > from rpy import * > ... > file = open("input.txt") > filecontent = file.read() > filecontent = r.tolower(filecontent) > words = r.strsplit(filecontent, " ") > wordsvector = r.unlist(words) > table = r.table(wordsvector) > ... > print table > print r.names(table) > > [1. 1. 1. 2. 3.] > None > > When I do something similar in R console, calling 'names(table)' returns > the name of the word for each frequency value. Why is this not going > that way in Python using rpy? > > Thanks in advance,
This is likely happening because rpy is converting the object on the fly (and is dropping the name information on the way, as python tuples/lists have no name for their elements). Turning off the conversion might be the way to solve this. With rpy2 this is tentatively more straightforward to achieve. Example with version 2.0.4: >>> import rpy2.robjects as ro >>> tab = ro.r["table"](ro.r["rpois"](100,5)) >>> print(tab) 0 1 2 3 4 5 6 7 8 9 10 11 2 6 10 12 12 20 14 19 2 1 1 1 >>> tuple(tab.names[0]) ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11') ------------------------------------------------------------------------------ Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT is a gathering of tech-side developers & brand creativity professionals. Meet the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp as they present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list