Daniel - 2 months ago 12x

R Question

Answer

If it is between each combination of rows,

```
t(combn(nrow(m), 2, FUN = function(i) m[i[1],]- m[i[2],]))
```

Or using `expand.grid`

to also include the difference of the same rows.

```
d1 <- expand.grid(1:nrow(m), 1:nrow(m))
rn <- do.call(paste, c(d1, sep=";"))
res <- t(apply(d1, 1, function(i) m[i[1],] - m[i[2],]))
row.names(res) <- rn
```

Source (Stackoverflow)

Comments