|
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/ ______________________________________________ [hidden email] 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. |
|
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 <[hidden email]> 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/ > > ______________________________________________ > [hidden email] 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/ ______________________________________________ [hidden email] 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. |
|
Hi Joshua,
Many thanks for your quick reply. > 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, 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 -- 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/ ______________________________________________ [hidden email] 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. |
|
On Mon, May 21, 2012 at 9:27 AM, Øystein Godøy <[hidden email]> wrote:
> Hi Joshua, > > Many thanks for your quick reply. > >> 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, > > 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. I cannot reproduce this behavior. On my system, the output has 100 elements. > 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/ -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/ ______________________________________________ [hidden email] 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 wrote on 2012-05-21
... > > 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. > > I cannot reproduce this behavior. On my system, the output has 100 > elements. ... 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 -- 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/ ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
