[R] matrix manipulations

Petr Savicky savicky at praha1.ff.cuni.cz
Tue Jan 18 18:27:46 CET 2011

On Mon, Jan 17, 2011 at 10:37:42PM +0000, Monica Pisica wrote:
> Hi,
> I've got 2 very good solutions, thank you very much. One, from Henrique Dallazuanna using the library reshape and one line of code - although it will take me quite some time to understand it. Here it is what he sent:
> library(reshape)
> xtabs(rowSums(cbind(value.x, value.y), na.rm = TRUE) ~ X1 + X2, merge(melt(m1), melt(m2), by = c('X1', 'X2'), all = TRUE), exclude = FALSE)
> The other is from Phil Spector ( code below) that i can understand quite easily, although until now to my shame i never quite used factor levels and their properties and i don't know their uses and possibilities. Until now i tried to avoid them and transform them in something else (like character strings).

Let me add a comment on factors.

Character vectors are easier to manipulate and factors carry additional
information. A balance between their uses depends on the application. A possible
strategy is to use character vectors, when the sequence of categorical data
should be manipulated, and convert to a factor, when needed. There is an R Wiki
page with some tips concerning factors at

Petr Savicky.

More information about the R-help mailing list