Re: [R] simple table/matrix problem
Hadley Wickham's reshape package makes tasks like these pretty easy. http://cran.r-project.org/web/packages/reshape/index.html -- View this message in context: http://r.789695.n4.nabble.com/simple-table-matrix-problem-tp2307969p2308713.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] simple table/matrix problem
Hi Given three vectors x - c(fish=3, dogs=5, bats=2) y - c(dogs=1, hogs=3) z - c(bats=3, dogs=5) How do I create a multi-way table like the following? out x y z bats 2 0 3 dogs 5 1 5 fish 3 0 0 hogs 0 3 0 ('out' is a matrix). See how the first line shows 'x' has 2 bats, 'y' has zero bats, and 'z' has 3 bats and so on for each line. The real application would have a matrix of size ~10 by ~1. -- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877 __ R-help@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.
Re: [R] simple table/matrix problem
Try this: Reduce(function(...)merge(..., by = 'ind', all = TRUE), lapply(list(x, y, z), stack)) On Fri, Jul 30, 2010 at 10:39 AM, Robin Hankin rk...@cam.ac.uk wrote: Hi Given three vectors x - c(fish=3, dogs=5, bats=2) y - c(dogs=1, hogs=3) z - c(bats=3, dogs=5) How do I create a multi-way table like the following? out x y z bats 2 0 3 dogs 5 1 5 fish 3 0 0 hogs 0 3 0 ('out' is a matrix). See how the first line shows 'x' has 2 bats, 'y' has zero bats, and 'z' has 3 bats and so on for each line. The real application would have a matrix of size ~10 by ~1. -- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877 __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@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.
Re: [R] simple table/matrix problem
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Robin Hankin Sent: Friday, July 30, 2010 6:40 AM To: r-help@r-project.org Subject: [R] simple table/matrix problem Hi Given three vectors x - c(fish=3, dogs=5, bats=2) y - c(dogs=1, hogs=3) z - c(bats=3, dogs=5) How do I create a multi-way table like the following? out x y z bats 2 0 3 dogs 5 1 5 fish 3 0 0 hogs 0 3 0 You could try using a matrix subscript to a matrix to insert the values, as in: f - function (dataList) { animals - sort(unique(a - unlist(lapply(dataList, names), use.names = FALSE))) variables - names(dataList) retval - array(0, dim = c(length(animals), length(variables)), dimnames = list(animals, variables)) retval[cbind(match(a, animals), rep(seq_along(dataList), vapply(dataList, length, integer(1] - unlist(dataList, use.names = FALSE) retval } f(list(x=x,y=y,z=z)) x y z bats 2 0 3 dogs 5 1 5 fish 3 0 0 hogs 0 3 0 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com ('out' is a matrix). See how the first line shows 'x' has 2 bats, 'y' has zero bats, and 'z' has 3 bats and so on for each line. The real application would have a matrix of size ~10 by ~1. -- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877 __ R-help@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. __ R-help@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.