R语言 来自两个(或更多)向量的所有元素的唯一组合

vyswwuz2  于 2022-12-06  发布在  其他
关注(0)|答案(6)|浏览(303)

我试图从R中两个不同大小的向量中创建所有元素的唯一组合。
例如,第一个向量为

a <- c("ABC", "DEF", "GHI")

第二种是当前以字符串形式存储的日期

b <- c("2012-05-01", "2012-05-02", "2012-05-03", "2012-05-04", "2012-05-05")

我需要创建一个包含两列的数据框,如下所示

> data
    a          b
1  ABC 2012-05-01
2  ABC 2012-05-02
3  ABC 2012-05-03
4  ABC 2012-05-04
5  ABC 2012-05-05
6  DEF 2012-05-01
7  DEF 2012-05-02
8  DEF 2012-05-03
9  DEF 2012-05-04
10 DEF 2012-05-05
11 GHI 2012-05-01
12 GHI 2012-05-02
13 GHI 2012-05-03
14 GHI 2012-05-04
15 GHI 2012-05-05

所以基本上,我在寻找一个唯一的组合,通过考虑一个向量(a)的所有元素,并置第二个向量(b)的所有元素。
理想的解决方案将推广到更多的输入向量。

另请参阅:

How to generate a matrix of combinations

hc2pp10m

hc2pp10m1#

这也许就是你想要的

> expand.grid(a,b)
   Var1       Var2
1   ABC 2012-05-01
2   DEF 2012-05-01
3   GHI 2012-05-01
4   ABC 2012-05-02
5   DEF 2012-05-02
6   GHI 2012-05-02
7   ABC 2012-05-03
8   DEF 2012-05-03
9   GHI 2012-05-03
10  ABC 2012-05-04
11  DEF 2012-05-04
12  GHI 2012-05-04
13  ABC 2012-05-05
14  DEF 2012-05-05
15  GHI 2012-05-05

如果产生的顺序不是您想要的,您可以稍后再排序。如果您将参数命名为expand.grid,它们会变成数据行名称:

df = expand.grid(a = a, b = b)
df[order(df$a), ]

并且expand.grid泛化为任意数量的输入列。

rm5edbpk

rm5edbpk2#

tidyr包提供了一个很好的替代函数crossing,它比经典的expand.grid函数运行得更好,因为(1)字符串没有转换成因子,(2)排序更直观:

library(tidyr)

a <- c("ABC", "DEF", "GHI")
b <- c("2012-05-01", "2012-05-02", "2012-05-03", "2012-05-04", "2012-05-05")

crossing(a, b)

# A tibble: 15 x 2
       a          b
   <chr>      <chr>
 1   ABC 2012-05-01
 2   ABC 2012-05-02
 3   ABC 2012-05-03
 4   ABC 2012-05-04
 5   ABC 2012-05-05
 6   DEF 2012-05-01
 7   DEF 2012-05-02
 8   DEF 2012-05-03
 9   DEF 2012-05-04
10   DEF 2012-05-05
11   GHI 2012-05-01
12   GHI 2012-05-02
13   GHI 2012-05-03
14   GHI 2012-05-04
15   GHI 2012-05-05
ljo96ir5

ljo96ir53#

r-faq概述中缺少data.table-包中的CJ-函数。使用:

library(data.table)
CJ(a, b, unique = TRUE)

给出:

a          b
 1: ABC 2012-05-01
 2: ABC 2012-05-02
 3: ABC 2012-05-03
 4: ABC 2012-05-04
 5: ABC 2012-05-05
 6: DEF 2012-05-01
 7: DEF 2012-05-02
 8: DEF 2012-05-03
 9: DEF 2012-05-04
10: DEF 2012-05-05
11: GHI 2012-05-01
12: GHI 2012-05-02
13: GHI 2012-05-03
14: GHI 2012-05-04
15: GHI 2012-05-05

注:自版本1.12.2起,CJ自动命名生成的列(另请参见herehere)。

aelbi1ox

aelbi1ox4#

从版本1.0.0开始,tidyr提供了它自己的expand.grid()版本。它是completes the existing family of expand() , nesting() , and crossing() with a low-level function that works with vectors
base::expand.grid()相比:
以最快的速度改变第一个元素。从不将字符串转换为因子。不添加任何附加属性。返回tibble,而不是数据框。可以展开任何广义向量,包括数据框。

a <- c("ABC", "DEF", "GHI")
b <- c("2012-05-01", "2012-05-02", "2012-05-03", "2012-05-04", "2012-05-05")

tidyr::expand_grid(a, b)

   a     b         
   <chr> <chr>     
 1 ABC   2012-05-01
 2 ABC   2012-05-02
 3 ABC   2012-05-03
 4 ABC   2012-05-04
 5 ABC   2012-05-05
 6 DEF   2012-05-01
 7 DEF   2012-05-02
 8 DEF   2012-05-03
 9 DEF   2012-05-04
10 DEF   2012-05-05
11 GHI   2012-05-01
12 GHI   2012-05-02
13 GHI   2012-05-03
14 GHI   2012-05-04
15 GHI   2012-05-05
wgxvkvu9

wgxvkvu95#

您可以使用排序函数对任意数量的列进行排序。

df <- expand.grid(a,b)
> df
   Var1       Var2
1   ABC 2012-05-01
2   DEF 2012-05-01
3   GHI 2012-05-01
4   ABC 2012-05-02
5   DEF 2012-05-02
6   GHI 2012-05-02
7   ABC 2012-05-03
8   DEF 2012-05-03
9   GHI 2012-05-03
10  ABC 2012-05-04
11  DEF 2012-05-04
12  GHI 2012-05-04
13  ABC 2012-05-05
14  DEF 2012-05-05
15  GHI 2012-05-05

> df[order( df[,1], df[,2] ),] 
   Var1       Var2
1   ABC 2012-05-01
4   ABC 2012-05-02
7   ABC 2012-05-03
10  ABC 2012-05-04
13  ABC 2012-05-05
2   DEF 2012-05-01
5   DEF 2012-05-02
8   DEF 2012-05-03
11  DEF 2012-05-04
14  DEF 2012-05-05
3   GHI 2012-05-01
6   GHI 2012-05-02
9   GHI 2012-05-03
12  GHI 2012-05-04
15  GHI 2012-05-05`
ffscu2ro

ffscu2ro6#

在base R中,可以尝试merge()、cbind()和expand.grid()。

a <- seq(1E4)
b <- c("2012-05-01", "2012-05-02", "2012-05-03", "2012-05-04", "2012-05-05")

 microbenchmark(
  "merge (1)" = mmm <- as.matrix(merge(a, b)),
  "diy (2)"   = {ccc <- cbind( rep(a, length(b)),
                               b[rep(seq_along(b), each = length(a))]
                        )
                },
 "diy R (3)"  = {ccc <- cbind( a,
                               b[rep(seq_along(b), each = length(a))]
                        )
                },
  "grid (4)"  = ggg <- expand.grid(a, b),
  times       = 2
)

输出。

Unit: milliseconds
      expr      min       lq     mean   median       uq      max neval
 merge (1) 863.3100 863.3100 888.6573 888.6573 914.0046 914.0046     2
   diy (2) 117.1912 117.1912 142.1394 142.1394 167.0875 167.0875     2
 diy R (3)  34.9320  34.9320  49.4119  49.4119  63.8918  63.8918     2
  grid (4)  45.1876  45.1876  46.1592  46.1592  47.1308  47.1308     2

相关问题