I'd like to use
RPy to try to manipulate a Python 2.3.4 list within R 2.4.1. My list is made up of five arrays (four of string-type — "utility", "target", "build" and "timeType" — and one float-type — "time").
My problem is that I can't seem to build a data frame in R. I'd like to, for example, group my data analysis by 'utility, 'target' and 'build' from calls made within Python.
When I use
r.data_frame() to create a data frame object, the resulting object is not an R data frame. The following prints "False" on the call from
r.is_data_frame():
==========
timeDataFrame = { "utility":[],
"target":[],
"build":[],
"timeType":[],
"time":[] }
for timeDataListObj in timeDataListArray:
for timeDataObj in timeDataListObj.timedata:
for timeDataType in timeDataTypes:
timeDataFrame["utility"].append(timeDataListObj.utility)
timeDataFrame["target"].append(timeDataListObj.target)
timeDataFrame["build"].append(timeDataListObj.build)
timeDataFrame["timeType"].append(timeDataType)
timeDataFrame["time"].append(float(timeValue))
df = r.data_frame(timeDataFrame["utility"], \
timeDataFrame["target"], \
timeDataFrame["build"], \
timeDataFrame["timeType"], \
timeDataFrame["time"])
r.print_(r.is_data_frame(df))
==========
Another problem I have is with syntax. For example, how can I perform a column reference like
df$target or
df$timeType?
When I tried to do either:
r.print_(df$target)
or
r.print_(df+r['$']+target)
or
r['print(df$target)']
I get syntax errors. Same with
r.split(df$target, df$build) and similar.
The problem seems to come down to how these are interpreted. Either Python misinterprets the
r.print_() calls and complains about the $ reference, or when I use
r['print(df$target)'], the R interpreter doesn't have any knowledge of the variable
df and complains about non-existent variables.
Any advice from seasoned Python/R/RPy users would be greatly appreciated. Thanks!
In fact, i've found that 90% of the time, it's easiest just to export the data, invoke R, read in and manipulate the data, export back out of R, and then pull the results back into my script. Yes, it's unwieldy, but it's kept me sane.
If that's not an option, try searching the RPy Mailing List Archives
posted by chrisamiller at 11:07 PM on February 25, 2008