2
votes

Sélection aléatoire de paires à partir d'un vecteur en R 153 fois

À partir d'un vecteur de 18 valeurs de texte, je veux générer 153 paires où aucune des paires n'est identique. Y a-t-il un moyen de faire cela avec la fonction exemple?

J'ai inclus ce que j'ai jusqu'à présent ci-dessous, c'est basique maintenant, je veux juste savoir comment construire dessus pour obtenir la fonction que je veux p>

#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)

r

5 commentaires

Ainsi, combn (18,2) génère l'ensemble exact de 153 paires d'entiers non dupliquées. Utilisez-les pour indexer (sous-ensemble) votre vecteur?


Vous avez en fait besoin de combinaisons , que vous pouvez obtenir avec combn (rewildingspps, m = 2) .


En fait, probablement juste combn (18,2, FUN = function (x) rewildingspps [x]) vous donnera ce que vous voulez directement?


@joran ou t (combn (rewildingspps, 2))


@avid_useR Oui, encore mieux!


3 Réponses :


1
votes
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"

0 commentaires

4
votes

La fonction combn a un argument pratique FUN :

combn(rewildingspps,2)

Ou encore mieux, à partir d'un commentaire ci-dessus:

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


0 commentaires

1
votes

En plus de la réponse de @ joran, puisque le titre dit "Sélection aléatoire des paires", nous pouvons randomiser les paires générées à partir de combn en échantillonner les indices (paires de combn sont classés en fonction de l'ordre du vecteur d'entrée, donc ce n'est pas aléatoire):

     [,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"  
...

Sortie:

XXX


0 commentaires