[R] Indexing to insert values from a dataframe into a matrix

Daisy Englert Duursma daisy.duursma at gmail.com
Wed Jun 29 03:29:12 CEST 2011


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



More information about the R-help mailing list