1

我有一个货币符号列表,但它们成对出现,不能自相矛盾。下面是一个例子:

ccy <- c("USD","EUR","JPY","GBP","CAD","AUD","NZD","CHF","NOK","SEK")
g <- expand.grid(ccy,ccy)
g <- g[g[,1]!=g[,2],]

但是美元兑欧元只是欧元兑美元的倒数,或者欧元兑日元只是日元兑欧元的倒数,所以我想从data.frame.

有没有一种程序化的方式只从列表中获取唯一的对?

4

1 回答 1

4

使用combn()

combn(ccy, 2)
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] "USD" "USD" "USD" "USD" "USD" "USD" "USD" "USD" "USD" "EUR" "EUR" "EUR" "EUR" "EUR" "EUR" "EUR"
[2,] "EUR" "JPY" "GBP" "CAD" "AUD" "NZD" "CHF" "NOK" "SEK" "JPY" "GBP" "CAD" "AUD" "NZD" "CHF" "NOK"
     [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32]
[1,] "EUR" "JPY" "JPY" "JPY" "JPY" "JPY" "JPY" "JPY" "GBP" "GBP" "GBP" "GBP" "GBP" "GBP" "CAD" "CAD"
[2,] "SEK" "GBP" "CAD" "AUD" "NZD" "CHF" "NOK" "SEK" "CAD" "AUD" "NZD" "CHF" "NOK" "SEK" "AUD" "NZD"
     [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
[1,] "CAD" "CAD" "CAD" "AUD" "AUD" "AUD" "AUD" "NZD" "NZD" "NZD" "CHF" "CHF" "NOK"
[2,] "CHF" "NOK" "SEK" "NZD" "CHF" "NOK" "SEK" "CHF" "NOK" "SEK" "NOK" "SEK" "SEK"

该函数从您的输入向量combn生成所有唯一的长度组合。n

有关?combn更多详细信息,请参阅。

于 2013-03-05T13:44:34.470 回答