#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Retreive factor values from R to Python


    Hello all,

    I am a Python beginner. I am trying to call R functions by using the rpy2 and rmagic modules.

    It works well but I have some difficulties to retrieve some special R outputs.

    Here is my problem: as a result in R (called s1), I have a data frame with 2 columns, 'sequence' and 'support'.

    'sequence' contains factors values, like : <{A}>, <{D},{B,F}>, etc.

    'support' contains numerical values.

    To import this result in Python, I use the following code:

    import rpy2
    import rmagic
    %load_ext rmagic

    R = %R as(s1, 'data.frame')

    The problem is the following :

    'support' data are OK, I retrieve the correct numerical values.

    Unfortunately, I can't manage 'sequence' data. Instead of values such as <{A}>, <{D},{B,F}>, Pyhton displays things like [ 4. , 8. , 16. , ....

    Could someone help me please?

    Thank you very much.

    JC
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    progress report


    I've installed r, it works.
    Have not yet gotten pyr2 or rpy2 whatever to work.

    This my progress against understanding your difficulty.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    Hi.

    First of all, thank you very much for trying to help me.

    Yes, R works on my computer too.

    The problem is when I am trying to make Python communicate with R.

    I process some stuffs in Python, then I need R to launch some specific tasks. That's why I use rpy2 and rmagic.

    The problem is that I would like to create a variable in Python, which comes from R (in my example I called this variable R, let's call it VAR for not being confusing). I create the variable in Python as follows :

    VAR = %R as(s1, 'data.frame')

    %R means I am communicating with R through the module rmagic.
    as(s1, 'data.frame') is the R syntax that creates the data frame to be imported in Python, and which contains the problematic factors <{A}>, <{D},{B,F}>, etc.

    VAR is then created in Python, but <{A}>, <{D},{B,F}>, etc. are not displayed as in R. Instead of these expected values, I get [ 4. , 8. , 16. , .... in Python.

    Maybe the problem is the definition of the data frame in Python, but I'm not enough aware of Python data definition to fix this issue....

    Is my problem clearer?

    Thanks a lot

    Michel
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    just for your information, I finally fixed the issue.
    Data as to be declared as matrix in R, and then they are properly imported in Python.
    Subject can be closed !

    Thanks again for your help.

    JC




    Originally Posted by JC_Huys
    Hi.

    First of all, thank you very much for trying to help me.

    Yes, R works on my computer too.

    The problem is when I am trying to make Python communicate with R.

    I process some stuffs in Python, then I need R to launch some specific tasks. That's why I use rpy2 and rmagic.

    The problem is that I would like to create a variable in Python, which comes from R (in my example I called this variable R, let's call it VAR for not being confusing). I create the variable in Python as follows :

    VAR = %R as(s1, 'data.frame')

    %R means I am communicating with R through the module rmagic.
    as(s1, 'data.frame') is the R syntax that creates the data frame to be imported in Python, and which contains the problematic factors <{A}>, <{D},{B,F}>, etc.

    VAR is then created in Python, but <{A}>, <{D},{B,F}>, etc. are not displayed as in R. Instead of these expected values, I get [ 4. , 8. , 16. , .... in Python.

    Maybe the problem is the definition of the data frame in Python, but I'm not enough aware of Python data definition to fix this issue....

    Is my problem clearer?

    Thanks a lot

    Michel
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    I'm glad you solved it. I still don't have the python interface to r installed.

    Thank you. I now have r which looks like a good system and I'll be more likely to try it. I sometimes use statistics and graphs.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    Yes, R is really a great tool. I hope you'll enjoy it !!!

    Best,

    JC


    Originally Posted by b49P23TIvg
    I'm glad you solved it. I still don't have the python interface to r installed.

    Thank you. I now have r which looks like a good system and I'll be more likely to try it. I sometimes use statistics and graphs.

IMN logo majestic logo threadwatch logo seochat tools logo