Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

Producing all combinations of two column values in R

I have a data.frame with two columns

> data.frame(a=c(5,4,3), b =c(1,2,4))
  a b
1 5 1
2 4 2
3 3 4

I want to produce a list of data.frames with different combinations of those column values; there should be a total of six possible scenarios for the above example (correct me if I am wrong):

  a b
1 5 1
2 4 2
3 3 4

  a b
1 5 1
2 4 4
3 3 2

  a b
1 5 2
2 4 1
3 3 4

  a b
1 5 2
2 4 4
3 3 1

  a b
1 5 4
2 4 2
3 3 1

  a b
1 5 4
2 4 1
3 3 2

Is there a simple function to do it? I don't think expand.grid worked out for me.

question from:https://stackoverflow.com/questions/65947343/producing-all-combinations-of-two-column-values-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually expand.grid can work here, but it is not recommended since it's rather inefficient when you have many rows in df (you need to subset n! out of n**n if you have n rows).

Below is an example using expand.grid

u <- do.call(expand.grid, rep(list(seq(nrow(df))), nrow(df)))
lapply(
  asplit(
    subset(
      u,
      apply(u, 1, FUN = function(x) length(unique(x))) == nrow(df)
    ), 1
  ), function(v) within(df, b <- b[v])
)

One more efficient option is to use perms from package pracma

library(pracma)
> lapply(asplit(perms(df$b),1),function(v) within(df,b<-v))
[[1]]
  a b
1 5 4
2 4 2
3 3 1

[[2]]
  a b
1 5 4
2 4 1
3 3 2

[[3]]
  a b
1 5 2
2 4 4
3 3 1

[[4]]
  a b
1 5 2
2 4 1
3 3 4

[[5]]
  a b
1 5 1
2 4 2
3 3 4

[[6]]
  a b
1 5 1
2 4 4
3 3 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...