R语言 计算组合和排列组合
组合学是数据分析和统计学的一个重要方面。它被用来解决许多基于能力的和现实生活中的问题。虽然排列组合确实考虑到了顺序,但组合却与之无关。因此,互换被认为是一种有序的组合。R语言允许我们调用许多包来计算组合和互换的能力。
方法1:Combinat包
R编程语言中的Combinat包可以用来计算数字的排列和组合。它提供了执行组合学的程序和方法。
R语言中属于该包的 combn() 方法用于生成x元素的所有组合,每次取m。 如果x是一个正整数,返回seq(x)元素的所有组合,每次取m。
语法
combn(x, m, fun=NULL, simplify=TRUE, …)
参数 :
- x – 组合的向量源
- m – 要取的元素的数量
- fun – 应用于每个组合的函数(可能为空)
- simplify – 逻辑的,如果是FALSE,返回一个列表,否则返回向量或数组
例1 :
# using required libraries
library(combinat)
# generating combinations of the
# alphabets taking 2 at a time
print ("Combination of letters two at a time")
combn(letters[1:4], 2)
输出
[1] "Combination of letters two at a time"
> combn(letters[1:4], 2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a" "a" "a" "b" "b" "c"
[2,] "b" "c" "d" "c" "d" "d"
例2 :
# using required libraries
library(combinat)
# generating combinations of the
# alphabets taking 2 at a time
print ("Combination where x=m")
# selecting 8 items out of 8
combn(8,8)
输出
[1] "Combination where x=m"
> #selecting 8 items out of 8
> combn(8,8)
[1] 1 2 3 4 5 6 7 8
下面的代码说明了m=1的方法,也就是说,所选项目的数量相当于1。由于每个项目可以被挑选一次,所以执行这个操作的方法数量相当于项目的总数。
例3 :
# using required libraries
library(combinat)
# generating combinations of
# the alphabets taking 2 at a time
print ("Combination where m=1")
# selecting 1 items out of 10
combn(10,1)
输出
[1] “Combination where m=1”
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 2 3 4 5 6 7 8 9 10
combn()方法还提供了一个将被生成的组合数量的计数作为输出。由于输出是以矩阵的形式获得的,最终的结果是矩阵中的列数,由 ncol(res) 给出,其中 res 是 combn() 方法的应用结果。
例4 :
# using required libraries
library(combinat)
# generating combinations of the
# alphabets taking 2 at a time
print ("Combination of letters two at a time")
res <- combn(letters[1:4], 2)
# calculate number of columns in
# the result
print ("Number of combinations : ")
print (ncol(res))
输出
[1] "Number of combinations : "
> print (ncol(res))
[1] 6
如果x是一个正整数,则返回seq(x)的所有元素的 排列组合 。数字0的排列组合是1。
语法
permn(x, fun=NULL, …)
参数 :
- x – 组合的向量源
- fun – 应用于每个排列组合的函数(可以是空的)。
例1 :
# using required libraries
library(combinat)
# generating permutations
# of the numbers 3
print ("Permutations of 3")
permn(3)
输出
[1] "Permutations of 3"
[[1]] [1] 1 2 3
[[2]] [1] 1 3 2
[[3]] [1] 3 1 2
[[4]] [1] 3 2 1
[[5]] [1] 2 3 1
[[6]] [1] 2 1 3
我们也可以计算一个向量或一个列表的排列组合。
例2 :
# using required libraries
library(combinat)
# declaring a list
x <- c('red', 'blue', 'green', 'violet')
print ("Permutations of vector x")
permn(x)
输出
[1] "Permutations of vector x"
> permn(x)
[[1]] [1] "red" "blue" "green" "violet"
[[2]] [1] "red" "blue" "violet" "green"
[[3]] [1] "red" "violet" "blue" "green"
[[4]] [1] "violet" "red" "blue" "green"
[[5]] [1] "violet" "red" "green" "blue"
[[6]] [1] "red" "violet" "green" "blue"
[[7]] [1] "red" "green" "violet" "blue"
[[8]] [1] "red" "green" "blue" "violet"
[[9]] [1] "green" "red" "blue" "violet"
[[10]] [1] "green" "red" "violet" "blue"
[[11]] [1] "green" "violet" "red" "blue"
[[12]] [1] "violet" "green" "red" "blue"
[[13]] [1] "violet" "green" "blue" "red"
[[14]] [1] "green" "violet" "blue" "red"
[[15]] [1] "green" "blue" "violet" "red"
[[16]] [1] "green" "blue" "red" "violet"
[[17]] [1] "blue" "green" "red" "violet"
[[18]] [1] "blue" "green" "violet" "red"
[[19]] [1] "blue" "violet" "green" "red"
[[20]] [1] "violet" "blue" "green" "red"
[[21]] [1] "violet" "blue" "red" "green"
[[22]] [1] "blue" "violet" "red" "green"
[[23]] [1] "blue" "red" "violet" "green"
[[24]] [1] "blue" "red" "green" "violet"
与combn()方法类似,也可以通过使用length()方法对permn()方法生成的输出向量来确定排列组合的数量。
例3 :
# using required libraries
library(combinat)
# declaring a list
x <- c('red', 'blue', 'green', 'violet')
print ("Permutations of vector x")
res <- permn(x)
print ("Number of possible permutations : ")
print (length(res))
输出
[1] “Number of possible permutations : ”
> print (length(res))
[1] 24
方法2:gtools包
使用R编程语言中的gtools包也可以用来计算没有重复和有重复的排列和组合。使用R语言中的gtools包可以很容易地进行组合学。
R语言中的permutations()方法可以用来轻松地计算有和无替换的排列组合。它返回一个数据框或一个矩阵,其中包含了根据约束条件对指定向量的元素进行洗牌后形成的可能排列组合。这种可能的排列组合的数量可以通过使用nrow()方法来获取,该方法返回获得的输出数据框中的行数。
语法
permutations(n , r, vec, repeats.allowed=F)
参数:
- n – 要选择的对象的数量
- r – 要选择的对象的数量
- vec – 要洗的原子向量或矩阵
- repeats.allowed – 默认情况下:false。如果为真,将生成允许重复的排列组合。
返回
一个带有可信的排列组合的数据框或矩阵。数据框中的行数等同于r的值。
例1 :
library(gtools)
# describing the input vector
vec<- LETTERS[4:7]
# getting permutations on choose 2
# letters out of 4 letters without
# replacement
res <- permutations(n=4,r=2,v=vec)
print ("Permutations without replacement")
print (res)
print ("Number of permutations without replacement")
print (nrow(res))
# calculating permutations with replacement
res1 <- permutations(n=4,r=2,v=vec,repeats.allowed=T)
print ("Permutations with replacement")
print (res1)
print ("Number of permutations with replacement")
print (nrow(res1))
输出
[1] "Permutations without replacement"
[,1] [,2]
[1,] "D" "E"
[2,] "D" "F"
[3,] "D" "G"
[4,] "E" "D"
[5,] "E" "F"
[6,] "E" "G"
[7,] "F" "D"
[8,] "F" "E"
[9,] "F" "G"
[10,] "G" "D"
[11,] "G" "E"
[12,] "G" "F"
[1] "Number of permutations without replacement"
[1] 12
[1] "Permutations with replacement"
[,1] [,2]
[1,] "D" "D"
[2,] "D" "E"
[3,] "D" "F"
[4,] "D" "G"
[5,] "E" "D"
[6,] "E" "E"
[7,] "E" "F"
[8,] "E" "G"
[9,] "F" "D"
[10,] "F" "E"
[11,] "F" "F"
[12,] "F" "G"
[13,] "G" "D"
[14,] "G" "E"
[15,] "G" "F"
[16,] "G" "G"
[1] "Number of permutations with replacement"
[1] 16
类似地,组合方法可以用来从指定的源向量生成可能的组合。所有的参数都与 permutations() 方法类似。
语法
combinations(n , r, vec, repeats.allowed=F)
不一定要指定输入向量。
例2 :
library(gtools)
# generating combinations of the
# alphabets taking 2 at a time
print ("Combination of five objects taken two at a time")
combinations(5, 2)
输出
[1] "Combination of five objects taken two at a time"
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 1 5
[5,] 2 3
[6,] 2 4
[7,] 2 5
[8,] 3 4
[9,] 3 5
[10,] 4 5
例3 :
library(gtools)
vec <- c(1:4)
# generating combinations of the
# digits taking 2 at a time
print ("Combination of four objects taken two at\
a time without repetition")
res<- combinations(n= 4, r = 2, v = vec)
print (res)
print ("Number of combinations without repetition")
print (nrow(res))
print ("Combination of four objects taken two at a \
time with repetition")
res1 <- combinations(n= 4, r = 2, v = vec, repeats.allowed=T)
print (res1)
print ("Number of combinations with repetition")
print (nrow(res1))
输出
[1] "Combination of four objects taken two at a time without repetition"
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4
[1] "Number of combinations without repetition"
[1] 6
[1] "Combination of four objects taken two at a time with repetition"
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
[5,] 2 2
[6,] 2 3
[7,] 2 4
[8,] 3 3
[9,] 3 4
[10,] 4 4
[1] "Number of combinations with repetition"
[1] 10