Skip to content

Changing selected elements of an array

5 messages · Joshua Wiley, Øystein Godøy

#
Hi!

I have a matrix defined on geographical positions (through) row and column 
names. I need to change a number of elements in this matrix using the 
information of a data.frame containing geographical positions and a number of 
variables.

Changing the value of one specific element is easy, but changing on a number 
of selected positions seems more difficult. When I use the geographical 
positions of the data.frame as index, blobs are changed and not individual 
elements.

How can I circumvent this feature of R?

E.g: in this situation I would like the downward diagonal to be changed, not 
the square box...
matrix(NA,nrow=9,ncol=10,dimnames=list(formatC(tmplat,format="f",digits=2),formatC(tmplon,format="f",digits=2)))
8.00 9.50 9.75 14.25 15.00 15.75 18.00 20.50 21.75 25.25
55.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
56.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
57.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
58.00   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
59.50   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
62.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
64.50   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
66.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
67.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
8.00 9.50 9.75 14.25 15.00 15.75 18.00 20.50 21.75 25.25
55.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
56.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
57.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
58.00   NA   NA   NA    NA   300   300   300    NA    NA    NA
59.50   NA   NA   NA    NA   300   300   300    NA    NA    NA
62.75   NA   NA   NA    NA   300   300   300    NA    NA    NA
64.50   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
66.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
67.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA

While I wanted:
8.00 9.50 9.75 14.25 15.00 15.75 18.00 20.50 21.75 25.25
55.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
56.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
57.75   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
58.00   NA   NA   NA    NA   300    NA   NA    NA    NA    NA
59.50   NA   NA   NA    NA    NA   300   NA    NA    NA    NA
62.75   NA   NA   NA    NA    NA    NA   300    NA    NA    NA
64.50   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
66.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA
67.25   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA

All the best
?ystein
#
Hi ?ystein,

You can do it by passing a matrix for indexing instead of two vectors.
 Here's an example:


tmpmat <- matrix(NA, nrow = 10, ncol = 10,
  dimnames = list(letters[1:10], LETTERS[1:10]))

tmpmat[cbind(c("d", "e", "f"), c("D", "E", "F"))] <- 100
tmpmat

The matrix is created using cbind() to columnwise bind the two vectors
together.  Then it does what you want I think.

Hope this helps,

Josh
On Mon, May 21, 2012 at 7:15 AM, ?ystein God?y <o.godoy at met.no> wrote:

  
    
#
Hi Joshua,

Many thanks for your quick reply.
This looks interesting and is what I want, but I am not fully understanding 
the output I receive. The input array has 100 elements while the resulting 
vector after replacement is 106 elements long. I have tried to understand the 
manual on this, but it is yet not obvious for me how I am supposed to handle 
this output.

All the best
?ystein
#
On Mon, May 21, 2012 at 9:27 AM, ?ystein God?y <o.godoy at met.no> wrote:
I cannot reproduce this behavior.  On my system, the output has 100 elements.

  
    
#
Joshua Wiley wrote on 2012-05-21
...
...
That is valuable information for me as your result is inline with what I 
understand from the manual. I am using R version 2.10.1 (2009-12-14) through 
the system implementation following Ubuntu Lucid. I have to look further into 
the cause for this. I just doublechecked your sample code on another system 
and got the same result there.

Thanks for your effort.

All the best
?ystein