从一个向量中随机选择对153次

时间:2019-06-27 16:33:03

标签: r

我想从18个文本值的向量中生成153个对,其中没有一个对完全相同。有没有办法使用示例函数来做到这一点?

我将下面介绍的内容包括在内,现在已经很基本了,我只想知道如何在此基础上构建所需的功能

#vector of species included in the survey
rewildingspps<c("lynx","wolf","wildcat","bison","beaver","moose","boar","owl","goshawk","osprey","nightheron","pelican","spoonbill","stork","eagle","bustard","crane","capercaillie")

#sample- will pick 2 species randomly from the rewildingspps vector
sample(rewildingspps,2)

3 个答案:

答案 0 :(得分:3)

combn函数有一个方便的FUN参数:

combn(18,2,FUN = function(x) rewildingspps[x])

或更妙的是,来自上面的评论:

combn(rewildingspps,2)

答案 1 :(得分:1)

matrix( unlist( combn( rewildingspps, 2 ) ), ncol = 2 )

       [,1]           [,2]          
  [1,] "lynx"         "owl"         
  [2,] "wolf"         "moose"       
  [3,] "lynx"         "goshawk"     
  [4,] "wildcat"      "moose"       
  [5,] "lynx"         "osprey"      
  [6,] "bison"        "moose"       
  [7,] "lynx"         "nightheron"  
  [8,] "beaver"       "moose"       
  [9,] "lynx"         "pelican"     
 [10,] "moose"        "moose"       
 [11,] "lynx"         "spoonbill"   
 [12,] "boar"         "moose"       
 [13,] "lynx"         "stork"       
 [14,] "owl"          "moose"       
 [15,] "lynx"         "eagle"       
 [16,] "goshawk"      "moose"       
 [17,] "lynx"         "bustard"     
 [18,] "osprey"       "moose"       
 [19,] "lynx"         "crane"       
 [20,] "nightheron"   "moose"       
 [21,] "lynx"         "capercaillie"
 [22,] "pelican"      "boar"        
 [23,] "lynx"         "owl"         
 [24,] "spoonbill"    "boar"        
 [25,] "lynx"         "goshawk"     
 [26,] "stork"        "boar"        
 [27,] "lynx"         "osprey"      
 [28,] "eagle"        "boar"        
 [29,] "lynx"         "nightheron"  
 [30,] "bustard"      "boar"        
 [31,] "lynx"         "pelican"     
 [32,] "crane"        "boar"        
 [33,] "lynx"         "spoonbill"   
 [34,] "capercaillie" "boar"        
 [35,] "wolf"         "stork"       
 [36,] "wildcat"      "boar"        
 [37,] "wolf"         "eagle"       
 [38,] "bison"        "boar"        
 [39,] "wolf"         "bustard"     
 [40,] "beaver"       "boar"        
 [41,] "wolf"         "crane"       
 [42,] "moose"        "boar"        
 [43,] "wolf"         "capercaillie"
 [44,] "boar"         "owl"         
 [45,] "wolf"         "goshawk"     
 [46,] "owl"          "owl"         
 [47,] "wolf"         "osprey"      
 [48,] "goshawk"      "owl"         
 [49,] "wolf"         "nightheron"  
 [50,] "osprey"       "owl"         
 [51,] "wolf"         "pelican"     
 [52,] "nightheron"   "owl"         
 [53,] "wolf"         "spoonbill"   
 [54,] "pelican"      "owl"         
 [55,] "wolf"         "stork"       
 [56,] "spoonbill"    "owl"         
 [57,] "wolf"         "eagle"       
 [58,] "stork"        "owl"         
 [59,] "wolf"         "bustard"     
 [60,] "eagle"        "owl"         
 [61,] "wolf"         "crane"       
 [62,] "bustard"      "owl"         
 [63,] "wolf"         "capercaillie"
 [64,] "crane"        "goshawk"     
 [65,] "wolf"         "osprey"      
 [66,] "capercaillie" "goshawk"     
 [67,] "wildcat"      "nightheron"  
 [68,] "bison"        "goshawk"     
 [69,] "wildcat"      "pelican"     
 [70,] "beaver"       "goshawk"     
 [71,] "wildcat"      "spoonbill"   
 [72,] "moose"        "goshawk"     
 [73,] "wildcat"      "stork"       
 [74,] "boar"         "goshawk"     
 [75,] "wildcat"      "eagle"       
 [76,] "owl"          "goshawk"     
 [77,] "wildcat"      "bustard"     
 [78,] "goshawk"      "goshawk"     
 [79,] "wildcat"      "crane"       
 [80,] "osprey"       "goshawk"     
 [81,] "wildcat"      "capercaillie"
 [82,] "nightheron"   "osprey"      
 [83,] "wildcat"      "nightheron"  
 [84,] "pelican"      "osprey"      
 [85,] "wildcat"      "pelican"     
 [86,] "spoonbill"    "osprey"      
 [87,] "wildcat"      "spoonbill"   
 [88,] "stork"        "osprey"      
 [89,] "wildcat"      "stork"       
 [90,] "eagle"        "osprey"      
 [91,] "wildcat"      "eagle"       
 [92,] "bustard"      "osprey"      
 [93,] "wildcat"      "bustard"     
 [94,] "crane"        "osprey"      
 [95,] "wildcat"      "crane"       
 [96,] "capercaillie" "osprey"      
 [97,] "bison"        "capercaillie"
 [98,] "beaver"       "nightheron"  
 [99,] "bison"        "pelican"     
[100,] "moose"        "nightheron"  
[101,] "bison"        "spoonbill"   
[102,] "boar"         "nightheron"  
[103,] "bison"        "stork"       
[104,] "owl"          "nightheron"  
[105,] "bison"        "eagle"       
[106,] "goshawk"      "nightheron"  
[107,] "bison"        "bustard"     
[108,] "osprey"       "nightheron"  
[109,] "bison"        "crane"       
[110,] "nightheron"   "nightheron"  
[111,] "bison"        "capercaillie"
[112,] "pelican"      "pelican"     
[113,] "bison"        "spoonbill"   
[114,] "spoonbill"    "pelican"     
[115,] "bison"        "stork"       
[116,] "stork"        "pelican"     
[117,] "bison"        "eagle"       
[118,] "eagle"        "pelican"     
[119,] "bison"        "bustard"     
[120,] "bustard"      "pelican"     
[121,] "bison"        "crane"       
[122,] "crane"        "pelican"     
[123,] "bison"        "capercaillie"
[124,] "capercaillie" "spoonbill"   
[125,] "beaver"       "stork"       
[126,] "moose"        "spoonbill"   
[127,] "beaver"       "eagle"       
[128,] "boar"         "spoonbill"   
[129,] "beaver"       "bustard"     
[130,] "owl"          "spoonbill"   
[131,] "beaver"       "crane"       
[132,] "goshawk"      "spoonbill"   
[133,] "beaver"       "capercaillie"
[134,] "osprey"       "stork"       
[135,] "beaver"       "eagle"       
[136,] "nightheron"   "stork"       
[137,] "beaver"       "bustard"     
[138,] "pelican"      "stork"       
[139,] "beaver"       "crane"       
[140,] "spoonbill"    "stork"       
[141,] "beaver"       "capercaillie"
[142,] "stork"        "eagle"       
[143,] "beaver"       "bustard"     
[144,] "eagle"        "eagle"       
[145,] "beaver"       "crane"       
[146,] "bustard"      "eagle"       
[147,] "beaver"       "capercaillie"
[148,] "crane"        "bustard"     
[149,] "beaver"       "crane"       
[150,] "capercaillie" "bustard"     
[151,] "moose"        "capercaillie"
[152,] "boar"         "crane"       
[153,] "moose"        "capercaillie"

答案 2 :(得分:1)

除了@joran的答案外,由于标题为“随机选择对”,因此我们可以combn对索引进行随机化处理,以sample生成的对(来自combn的对是根据输入向量的顺序排序,因此不是随机的):

pairs <- t(combn(rewildingspps, 2))
pairs[sample(1:nrow(pairs), nrow(pairs)),]

输出:

     [,1]         [,2]          
  [1,] "boar"       "stork"       
  [2,] "wildcat"    "stork"       
  [3,] "owl"        "eagle"       
  [4,] "wolf"       "boar"        
  [5,] "goshawk"    "pelican"     
  [6,] "wildcat"    "beaver"      
  [7,] "osprey"     "nightheron"  
  [8,] "lynx"       "spoonbill"   
  [9,] "lynx"       "nightheron"  
 [10,] "lynx"       "osprey"      
 [11,] "owl"        "spoonbill"   
 [12,] "owl"        "nightheron"  
 [13,] "moose"      "bustard"     
 [14,] "goshawk"    "capercaillie"
 [15,] "wolf"       "stork"       
 [16,] "pelican"    "stork"       
 [17,] "nightheron" "spoonbill"   
 [18,] "osprey"     "pelican"     
 [19,] "osprey"     "crane"       
 [20,] "spoonbill"  "bustard"  
...