Hello, I'm trying to create a matrix (95x55) with data from a data.frame pop: xloc yloc go ind Ene totW 1 23 20 516 1 0.02 20.21 2 23 20 1143 1 0.02 20.21 3 23 20 250 1 0.02 20.21 4 22 15 251 1 0.02 18.69 5 22 15 598 1 0.02 18.69 6 21 19 250 1 0.02 20.21 7 22 20 251 1 0.02 18.69 8 22 20 598 1 0.02 18.69 where xloc is the x dimension, yloc the y dimension, and totW the values for each cell (xloc,yloc) in my matrix (55x95): I'm trying: Bio=with(pop, (table(factor(totW, levels = 1:55), factor(totW, levels = 1:95)))) But I don't get what I want. Hints? Thanks! Nic
On Mar 19, 2011, at 5:29 PM, Nicolas Gutierrez wrote:> Hello, > > I'm trying to create a matrix (95x55) with data from a data.frame pop: > > xloc yloc go ind Ene totW > 1 23 20 516 1 0.02 20.21 > 2 23 20 1143 1 0.02 20.21 > 3 23 20 250 1 0.02 20.21 > 4 22 15 251 1 0.02 18.69 > 5 22 15 598 1 0.02 18.69 > 6 21 19 250 1 0.02 20.21 > 7 22 20 251 1 0.02 18.69 > 8 22 20 598 1 0.02 18.69 > > where xloc is the x dimension, yloc the y dimension, and totW the > values for each cell (xloc,yloc) in my matrix (55x95): > > I'm trying: > > Bio=with(pop, (table(factor(totW, levels = 1:55), factor(totW, > levels = 1:95))))You probably should be using `cut` rather than `factor`. (Both will create factor-class variables.) Factor will give you a different level at each unique value while it appears that you want integer valued "cuts". It's also unclear why you would be applying these cuts to totW twice???? You have three rows with identical (23,20) x,y coordinates and two each for (22,15) and (22,20), although the go values are different.> > But I don't get what I want. Hints? Thanks!Better advice might follow if you said what you were really trying to do and what the final answer should look like.> > Nic > > ______________________________________________ > 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
Here is one way to fill in the value using indexing; it appears that you data has the same xloc/yloc values> xxloc yloc go ind Ene totW 1 23 20 516 1 0.02 20.21 2 23 20 1143 1 0.02 20.21 3 23 20 250 1 0.02 20.21 4 22 15 251 1 0.02 18.69 5 22 15 598 1 0.02 18.69 6 21 19 250 1 0.02 20.21 7 22 20 251 1 0.02 18.69 8 22 20 598 1 0.02 18.69> x.mat <- matrix(NA, 95, 55) # create matrix > x.mat[cbind(x$xloc, x$yloc)] <- x$totW > which(!is.na(x.mat), arr.ind =TRUE)row col [1,] 22 15 [2,] 21 19 [3,] 22 20 [4,] 23 20>On Sat, Mar 19, 2011 at 5:29 PM, Nicolas Gutierrez <nicolasg at uw.edu> wrote:> Hello, > > I'm trying to create a matrix (95x55) with data from a data.frame pop: > > ?xloc yloc ? ? ?go ?ind ? ?Ene ? ? totW > ?1 ? ?23 ?20 ? 516 ? 1 ? ? 0.02 ? 20.21 > ?2 ? ?23 ?20 ?1143 ? 1 ? ? 0.02 ? 20.21 > ?3 ? ?23 ?20 ? 250 ? 1 ? ? 0.02 ? 20.21 > ?4 ? ?22 ?15 ? 251 ? 1 ? ? 0.02 ? 18.69 > ?5 ? ?22 ?15 ? 598 ? 1 ? ? 0.02 ? 18.69 > ?6 ? ?21 ?19 ? 250 ? 1 ? ? 0.02 ? 20.21 > ?7 ? ?22 ?20 ? 251 ? 1 ? ? 0.02 ? 18.69 > ?8 ? ?22 ?20 ? 598 ? 1 ? ? 0.02 ? 18.69 > > where xloc is the x dimension, ?yloc the y dimension, and totW the values > for each cell (xloc,yloc) in my matrix (55x95): > > I'm trying: > > Bio=with(pop, (table(factor(totW, levels = 1:55), factor(totW, levels > 1:95)))) > > But I don't get what I want. Hints? Thanks! > > Nic > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Thanks Jim and David.. that was easy! Nic On 3/19/2011 3:00 PM, jim holtman wrote:> x.mat<- matrix(NA, 95, 55) # create matrix >> x.mat[cbind(x$xloc, x$yloc)]<- x$totW >> which(!is.na(x.mat), arr.ind =TRUE)