Rewarp - 1 year ago 43

R Question

I am trying to create a data frame that pulls combinations of values from a data frame, and applies a function on the data, and finally assigning the label of the combinations as identifiers in the data frame.

To create a simple mockup of the data:

`A = (1:20)`

B = seq(1, 40, 2)

To create a mockup of each identifier:

`L = c("q", "r", "q", "p", "q", "p", "r", "r", "r", "r", "r", "r", "r", "r", "o", "p", "q", "r", "r", "q")`

The mockup data frame is then created with:

`dat.example = data.frame(A, B, L)`

Here, the data in each row is subtracted by the data from the previous row, with all possible combinations.

`diffA = combn(dat.example$A, 2, FUN = diff)`

diffB = combn(dat.example$B, 2, FUN = diff)

After all possible combination of differences within columns

`A`

`B`

`calc.diff = sqrt((diffA^2) + (diffB^2))`

Now comes the tricky part, where I pull out the identifiers for the combinations.

`(L.list = combn(dat.example$L, 2, simplify = F))`

L.list.1 = sapply(L.list, "[[", 1)

L.list.2 = sapply(L.list, "[[", 2)

L.dat = data.frame(

X = L.list.1,

Y = L.list.2

)

L.dat$comp = with(

L.dat,

interaction(X, Y, sep = "_")

)

And combine them into a master dataset

`master = data.frame(`

calc.diff,

L.dat$comp

)

Is there a simpler way to perform this that I am missing?

Answer Source

Based on Jay's comement for the "tricky part":

```
L.dat.comp <- combn(dat.example$L, 2, paste0, collapse = "_", simplify=F)
master <- data.frame(cbind(calc.diff,unlist(L.dat.comp)))
```

So the whole thing looks like:

```
A = (1:20)
B = seq(1, 40, 2)
L = c("q", "r", "q", "p", "q", "p", "r", "r", "r", "r", "r", "r", "r", "r", "o", "p", "q", "r", "r", "q")
dat.example = data.frame(A, B, L)
diffA = combn(dat.example$A, 2, FUN = diff)
diffB = combn(dat.example$B, 2, FUN = diff)
calc.diff = sqrt((diffA^2) + (diffB^2))
L.dat.comp <- combn(dat.example$L, 2, paste0, collapse = "_", simplify=F)
master <- data.frame(cbind(calc.diff,unlist(L.dat.comp)))
```