Changing selected elements of an array
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! 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...
tmpmat <-
matrix(NA,nrow=9,ncol=10,dimnames=list(formatC(tmplat,format="f",digits=2),formatC(tmplon,format="f",digits=2)))
tmpmat
? ? ?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
tmpmat[c("58.00","59.50","62.75"),c("15.00","15.75","18.00")] <- 300
tmpmat
? ? ?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:
tmpmat
? ? ?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 -- Dr. Oystein Godoy Norwegian Meteorological Institute P.O.BOX 43, Blindern, N-0313 OSLO, Norway Ph: (+47) 2296 3000 (switchb) 2296 3334 (direct line) Fax:(+47) 2296 3050 Institute home page: http://met.no/
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/