Skip to content
Prev 305363 / 398506 Next

how to create a substraction matrix (subtract a row of every column from the same row in other columns)

Hello,

I'm not sure if this is what you want.

dat <- data.frame(matrix(1:25 + rnorm(25), ncol=5,
     dimnames=list(NULL, paste0("x", 1:5))))

d2 <- apply(dat, 1, diff)
nms <- t(outer(names(dat), names(dat), paste0))

res <- data.frame(difftype = nms[lower.tri(nms)],
     value = d2[lower.tri(d2, diag = TRUE)])
res

Hope this helps,

Rui Barradas

Em 12-09-2012 21:45, csmeredith escreveu: