Thanks Kent that helps some, at least i can do the basic stuff i can do in R
now.

But you kinda hit the nail on the head with your statement "This seems to
work, it keeps a in the internal R representation instead
of converting it to a list of lists" This all started with me trying to get
R to do a kmeans algorithm on a list of lists (a list composed of vectors
containing integers). What i want to do is convert a python list of lists to
the approperiate R object so that i can use the r.kmeans algorithm on it.
I'll write the basic program below.

As i stated i think the conversion from Python to R is going wrong, but i
have no clue on how to properly address that.
The code snippet i was talking about is on page 15 and 16 of the rpy
reference guide http://rpy.sourceforge.net/rpy/doc/rpy.pdf ; the examples
just don't work and i am lacking enough python experience to see why :-S.

What i'm trying to do now is :

----
from rpy import *

class Test:
   def as_r(self):
       return [[1,2,3,4,5],[2,3,4,5,1],[3,4,5,1,2],[4,5,1,2,3],[5,1,2,3,4]]

if __name__ == "__main__":
   a=with_mode(NO_CONVERSION, Test)()
   r.kmeans(a, 2, 5, 10, "Forgy")

----
Which gives as a result :
----
RHOME= C:\Program Files\R\R-2.4.0
RVERSION= 2.4.0
RVER= 2040
RUSER= C:\Documents and Settings\Ronald
Loading the R DLL C:\Program Files\R\R-2.4.0\bin\R.dll .. Done.
Loading Rpy version 2040 .. Done.
Creating the R object 'r' ..  Done
Traceback (most recent call last):
 File "rpy-test2.py", line 9, in ?
   r.kmeans(a, 2, 5, 10, "Forgy")
rpy.RException: Error in as.double.default(x) : (list) object cannot be
coerced
to 'double'
----
Hope you can shed more light on it.

Many thanx for your efforts - Geofram


On 12/21/06, Kent Johnson <[EMAIL PROTECTED]> wrote:

Geoframer wrote:
> R is a statistical language and Rpy is the python interface for it.
> However somehow I'm failing to see a step in the python code with which
I
> address the R language.
>
> in R I can do :
>
> a=diag(10)                              #produces an identity matrix of
> size 10
> b=kmeans(a,2,5,10,"Forgy")    #calculate a kmeans clustering algorithm
> on the 10 vectors contained by the matrix just declared.
>
>
> in Ipython this does :
>
> ---------
> In [1]: from rpy import *
> RHOME= C:\Program Files\R\R-2.4.0
> RVERSION= 2.4.0
> RVER= 2040
> RUSER= C:\Documents and Settings\Ronald
> Loading the R DLL C:\Program Files\R\R-2.4.0\bin\R.dll .. Done.
> Loading Rpy version 2040 .. Done.
> Creating the R object 'r' ..  Done
>
> In [2]: a = r.diag(10)
>
> In [3]: b = r.kmeans(a,2,10,5,"Forgy")
>
---------------------------------------------------------------------------
> rpy.RException                                Traceback (most recent
> call last)
>
> C:\Python24\<ipython console>
>
> RException: Error in as.double.default(x) : (list) object cannot be
> coerced to '
> double'
> ---------

This seems to work, it keeps a in the internal R representation instead
of converting it to a list of lists:

In [1]: from rpy import *
RHOME= C:\Program Files\R\R-2.3.1
RVERSION= 2.3.1
RVER= 2031
RUSER= G:\
Loading the R DLL C:\Program Files\R\R-2.3.1\bin\R.dll .. Done.
Loading Rpy version 2031 .. Done.
Creating the R object 'r' ..  Done

In [22]: aa=with_mode(NO_CONVERSION, r.diag)(10)

In [25]: b=r.kmeans(aa,2,10,5,"Forgy")

In [26]: b
Out[26]:
{'centers': [[0.1111111111111111,
               0.1111111111111111,
               0.1111111111111111,
               0.1111111111111111,
               0.0,
               0.1111111111111111,
               0.1111111111111111,
               0.1111111111111111,
               0.1111111111111111,
               0.1111111111111111],
              [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]],
  'cluster': [1, 1, 1, 1, 2, 1, 1, 1, 1, 1],
  'size': [9, 1],
  'withinss': [8.0000000000000018, 0.0]}

> I've tried numerous things to get it to work, but i basically can not
> find out how i do something as simple as the two statements in R in
> RPython. Apparently something is going wrong somewhere in the conversion
> of python objects to R objects but i can't seem to fix it. There is a
> code snippet in the RPy-reference manual but it's only valid for python
> 2.2 and 2.1 and i couldn't get it to work on 2.4.

Where is the snippet?

Cheers,
Kent


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to