89397

Pass list from python to R through rpy2

Question:

I'm trying to make a simple call to a R package (ks) from within python through <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/index.html" rel="nofollow">rpy2</a>. This is what I would like to achieve:

import rpy2.robjects as robjects # Define two matrices. matrix1 = [[1,1,1,1], [3,3,3,3]] matrix2 = [[1,1,1,1], [3,3,3,3]] # Call 'ks' function to obtain p_value. p_val = robjects.r(''' library(ks) kde.test(x1=matrix1, x2=matrix2)$pvalue''') print p_val

I tried following the <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/introduction.html#calling-r-functions" rel="nofollow">documentation from rpy2</a> but it is very scarce. Any help would be appreciated.

Answer1:

Posting my own answer based on the one given by <a href="https://stackoverflow.com/a/19737834/1391441" rel="nofollow">lgautier</a> since that one did not work as is. I also made it a bit more general by passing nrow instead of having it fixed.

import rpy2.robjects as robjects from rpy2.robjects.packages import importr ks = importr('ks') kde_test = ks.kde_test matr1 = [1., 3., 1., 3., 0.2, 1.5, 0.5, 1.3] matr2 = [1., 3., 1., 3., 0.2, 1.5, 0.5, 1.3, 0.5, 4.6] m1 = robjects.r.matrix(robjects.FloatVector(matr1), nrow=int(len(matr1)/2), byrow=True) m2 = robjects.r.matrix(robjects.FloatVector(matr2), nrow=int(len(matr2)/2), byrow=True) res = kde_test(x1 = m1, x2 = m2) pval = res.rx2('pvalue') print float(str(pval)[4:])

All credit goes to lgautier for proposing the solution even if it did not work at first.

Answer2:

To get <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/robjects_rpackages.html#rpy2.robjects.packages" rel="nofollow">functions in packages</a>:

from rpy2.robjects.packages import importr ks = importr('ks') kde_test = ks.kde_test

To <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/vector.html#matrix" rel="nofollow">build matrices</a>:

import rpy2.robjects Matrix = rpy2.robjects.r.matrix from rpy2.robjects.vectors import IntVector matrix1 = Matrix( IntVector([1,1,1,1, 3,3,3,3]), nrow=2) matrix2 = Matrix( IntVector([1,1,1,1, 3,3,3,3]), nrow=2)

To <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/robjects_functions.html" rel="nofollow">call functions</a>:

res = kde_test(x1 = matrix1, x2 = matrix2)

To <a href="http://rpy.sourceforge.net/rpy2/doc-2.3/html/vector.html#extracting-r-style" rel="nofollow">extract a named element in a list</a>:

pval = res.rx2('pvalue')

Recommend

  • Avoid object slicing for non-virtual destructors
  • How can I turn off registry redirection on Python?
  • Python argparse: list individual choices in the usage
  • How do I get the monitor of an active window in GNOME?
  • Slow Group_concat query
  • How to extract value from one S4 class
  • How to plot predictions of binomial GLM that has both continuous and categorical variables
  • jQuery calculation NaN
  • the five top rows of the oval are erased. why?
  • Force ggplot legend to show all categories when no values are present [duplicate]
  • How to move object along the polygons
  • Java Circle-Circle Collision Detection
  • No projects found to import
  • Adding/Removing Lines from a JPanel
  • Creating a layer of gradient within an SVG path dynamically
  • TextBox AutoCompleteStringCollection Suggest
  • How do i disable a text box within an iframe
  • Outputting SharePoint Hyperlink Column as URL
  • In matplotlib, how do you change the fontsize of a single figure?
  • converting text file into xml using php?
  • Do I need to reset a Perl hash index?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Custom Tabgroup Appcelerator
  • Django rest serializer Breaks when data exists
  • Is it possible to access block's scope in method?
  • Checking free space on FTP server
  • Can I check if a recipient has an automatic reply before I send an email?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Java applet as stand-alone Windows application?
  • Jquery - Jquery Wysiwyg return html as a string
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Arrays break string types in Julia
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • Java static initializers and reflection
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases