Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMagic, IPython and Summary Information

Following the example here

http://www.randalolson.com/2013/01/14/filling-in-pythons-gaps-in-statistics-packages-with-rmagic/

I tried the same on a different data set found here, in an IPython notebook.

https://github.com/burakbayramli/kod/blob/master/delltest/dell.tgz

from pandas import *
orders = read_csv("dell.csv",sep=",")
%load_ext rmagic
%R -i orders print(summary(orders))

I get

     Length Class  Mode
[1,] 25     -none- list
[2,] 25     -none- list
[3,] 25     -none- list
..

However the same in R

data <- read.csv ("dell.csv",header=TRUE,sep=",")
print (summary(data))

gives me the correct summary information.

      rank        per_customer_count total_total_amount    orderid     
 Min.   : 1.000   Min.   : 1.000     Min.   :    0.14   Min.   :    1  
 1st Qu.: 2.000   1st Qu.: 6.000     1st Qu.:  866.11   1st Qu.: 2964  
 Median : 4.000   Median : 8.000     Median : 1764.08   Median : 5980  
 Mean   : 4.997   Mean   : 9.426     Mean   : 2004.95   Mean   : 5987  
 3rd Qu.: 7.000   3rd Qu.:12.000     3rd Qu.: 2856.06   3rd Qu.: 9004  
 ...

Any ideas?

like image 457
BBSysDyn Avatar asked Nov 04 '22 06:11

BBSysDyn


1 Answers

I had a quick look and there appear to be a number of situations in which the ipython magic is not getting the conversion right. I have to get in touch with them regarding rmagic and more magic.

In the meantime, you should be able to cook up what you need to progress from the snippet of code below:

import pandas
orders = pandas.read_csv("dell.csv", sep=",")
%load_ext rmagic

import rpy2.robjects
d = dict()
for i, (k,v) in enumerate(orders.iteritems()):
    print("%s (type: %s - %i/%i)" %(k, v.dtype.kind, i, orders.shape[1]))
    if v.dtype.kind == 'O':
      v = rpy2.robjects.vectors.StrVector(v)
    d[k] = rpy2.robjects.conversion.py2ri(v)
df = rpy2.robjects.DataFrame(d)

def print_rsummary(x):
    print(rpy2.robjects.baseenv['summary'](x))

print_rsummary(df)
like image 181
lgautier Avatar answered Nov 08 '22 11:11

lgautier