Daisy Englert Duursma
2011-Jun-29 01:18 UTC
[R] Indexing to Insert values from a dataframe into a matrix
Hello, I think this is a simple problem but I am not coming up with a simple solution. I think it just an indexing problem. I can easily replace values in a matrix from a dataframe when the dataframe has row and column numbers. In the example below I use row and column names and I can not get it to work #make a matrix where rows and columns are the lat and long for a bounding box of Australia and all elements have the value of -9990 bb<-matrix(c(rep(-9999,691*886)),nrow=691 ,ncol=886,dimnames=list(seq(-10,-44.50,by=-0.05),seq(112,156.25,by=0.05))) #dfr with row names and col names and values to be replaced in the matrix dfr <- data.frame(cbind(x=seq(120,125,by=0.05), y=-25, var.1=1)) #insert the values from the dfr into the matrix bb[dfr$x,dfr$y]<-d$var.1 Thanks for your help, Daisy
Daisy Englert Duursma
2011-Jun-29 01:29 UTC
[R] Indexing to insert values from a dataframe into a matrix
Hello, I think this is a simple problem but I am not coming up with a simple solution. I think it just an indexing problem. I can easily replace values in a matrix from a dataframe when the dataframe has row and column numbers. In the example below I use row and column names and ?I can not get it to work #make a matrix where rows and columns are the lat and long for a bounding box of Australia and all elements have the value of -9990 bb<-matrix(c(rep(-9999,691*886)),nrow=691 ,ncol=886,dimnames=list(seq(-10,-44.50,by=-0.05),seq(112,156.25,by=0.05))) #dfr with row names and col names and values to be replaced in the matrix dfr <- data.frame(cbind(x=seq(120,125,by=0.05), y=-25, var.1=1)) #insert the values from the dfr into the matrix bb[dfr$x,dfr$y]<-d$var.1 Thanks for your help, Daisy -- Daisy Englert Duursma Department of Biological Sciences Room E8C156 Macquarie University, North Ryde, NSW 2109 Australia Tel +61 2 9850 9256
David Winsemius
2011-Jun-29 01:47 UTC
[R] Indexing to Insert values from a dataframe into a matrix
On Jun 28, 2011, at 9:18 PM, Daisy Englert Duursma wrote:> Hello, > > I think this is a simple problem but I am not coming up with a simple > solution. I think it just an indexing problem. > > I can easily replace values in a matrix from a dataframe when the > dataframe has row and column numbers. In the example below I use row > and column names and I can not get it to work > > #make a matrix where rows and columns are the lat and long for a > bounding box of Australia and all elements have the value of -9990 > > bb<-matrix(c(rep(-9999,691*886)),nrow=691 > ,ncol > =886,dimnames=list(seq(-10,-44.50,by=-0.05),seq(112,156.25,by=0.05))) > > #dfr with row names and col names and values to be replaced in the > matrix > > dfr <- data.frame(cbind(x=seq(120,125,by=0.05), y=-25, var.1=1)) > > #insert the values from the dfr into the matrix > bb[dfr$x,dfr$y]<-d$var.1Indexing with a two column matrix is possible, but those vectors are not legitimate indices even if you bundle them into a two column matrix. (dfr$y's are all -25, so at least the error report is informative). These will return indices: sapply(as.character(dfr$y), function(x) which(x== dimnames(bb)[[1]])) sapply(as.character(dfr$x), function(x) which(x== dimnames(bb)[[2]])) So try: > #insert the values from the dfr into the matrix > bb[matrix(c(sapply(as.character(dfr$y), function(x) which(x== dimnames(bb)[[1]])), sapply(as.character(dfr$x), function(x) which(x== dimnames(bb)[[2]]))), ncol=2)] <- dfr[["var.1"]]> > Thanks for your help, > Daisy > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT