The code snippet I gave was incorrect:

# ---
import rpy2.robjects as ro
import rpy2.rlike.container as rlc

m = ro.r.matrix(range(9), nrow=3, ncol=3)
print(m)

idx = rlc.TaggedList([ro.IntVector([1, ]),
                       ro.IntVector([2, ])])
m2 = m.assign(idx, 33)
print(m2)

idx = rlc.TaggedList([ro.IntVector([1, 2, 3]),
                       ro.IntVector([1, 2, 3])])
m3 = m.assign(idx, 33)
print(m3)

idx = rlc.TaggedList([ro.r.cbind(ro.IntVector([1, 2, 3]),
                                  ro.IntVector([1, 2, 3])),
                       ])
m4 = m.assign(idx, 33)
print(m4)
# ---

That complication is due to the fact that the R function "[<-" is called 
behind the hood.
It has the advantage that specific signatures to "[<-" will be handled 
gracefully, the disadvantage is the verbosity.
The other potential issue is the copying (I have ideas for that).


You could also consider going the numpy route:

import numpy

# using the 'm' from the previous code snippet
nm = numpy.asarray(m)
nm.shape = (3,3)
nm.strides = nm.strides[::-1]

print(nm)

# zero-offset (while R has one-offset)
nm[1-1][2-1] = 33

print(nm)





L.





Sancar Adali wrote:
> If I modify the code in the following way
> m = ro.r['matrix'](range(4), nrow=2, ncol=2)
> 
>     idx = ro.IntVector([1,4])
>     m2 = m.assign(idx, 33)
> 
>     print(m2)
> 
> I get the following matrix
> 
>      [,1] [,2]
> [1,] 33   2
> [2,] 1    33
> 
> I think the indices are like vector indices here, a single index, In
> this case 1 and 4 is assigned to 33 which are [1,1] and [2,2]
> respectively . Is there a way to access the matrix elements using pair
> of indices?
> 
> On Sat, Feb 21, 2009 at 2:46 PM, Laurent Gautier <lgaut...@gmail.com> wrote:
>> Sancar Adali wrote:
>>> I'm trying to access and update a particular element of a matrix to update
>>> it
>>>
>>> for example in R,
>>>
>>> mat=matrix(0,nrow=2,ncol=2)
>>> mat[0,0]= mat[0,0]+1
>> In R, vector indexing starts at one; zero are silently ignored.
>>
>>> How do I do this in rpy2
>>> do I have to use the low-level interface or can I use high-level interface
>>>
>> Using the high-level interface is possible.
>>
>> One way is:
>>
>> import rpy2.robjects as ro
>>
>> m = ro.r['matrix'](range(4), nrow=2, ncol=2)
>>
>> idx = ro.IntVector([1,1])
>> m2 = m.assign(idx, 33)
>>
>> print(m2)
>>
>>
>> Not as elegant as one could wish... the 2.1 series for rpy2 will hopefully
>> have something nicer.
>>
>>
>> L.
>>
>> PS: using the rpy2-numpy compatibility layer is an another way.
>>
> 
> 
> 


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to