计算 R 中的组合和排列
组合学是数据分析和统计的一个重要方面。它用于解决许多基于能力的和现实生活中的问题。虽然排列确实考虑了顺序,但组合是独立的。因此,排列被认为是有序组合。 R 语言使我们能够调用许多包来计算组合和排列。
方法一:组合包
R 编程语言中的 Combinat 包可用于计算数字的排列和组合。它提供了执行组合学的例程和方法。
属于该包的 R 语言中的 combn()方法用于生成一次取 m 个 x 元素的所有组合。如果 x 是正整数,则返回 seq(x) 一次取 m 个元素的所有组合。
Syntax:
combn(x, m, fun=NULL, simplify=TRUE, …)
Parameters :
- x – vector source for combinations
- m – number of elements to be taken
- fun – function to be applied to each combination (may be null)
- simplify – logical, if FALSE, returns a list, otherwise returns vector or array
示例 1:
R
# 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)
R
# 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)
R
# 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)
R
# 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))
R
# using required libraries
library(combinat)
# generating permutations
# of the numbers 3
print ("Permutations of 3")
permn(3)
R
# using required libraries
library(combinat)
# declaring a list
x <- c('red', 'blue', 'green', 'violet')
print ("Permutations of vector x")
permn(x)
R
# 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))
R
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))
R
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)
R
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 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
R 中的 permn()方法生成 x 元素的所有排列。如果 x 是正整数,则返回 seq(x) 元素的所有排列。数字 0 的排列是 1。
Syntax:
permn(x, fun=NULL, …)
Parameters :
- x – vector source for combinations
- fun – function to be applied to each permutation (may be null)
示例 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() 方法类似,也可以通过对 permn() 方法生成的输出向量使用 length() 方法来确定排列数。
示例 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
方法二:gtools包
使用 R 编程语言中的 gtools 包也可以轻松计算无重复和有重复的排列和组合。使用 R 中的 gtools 包可以轻松实现组合数学。
R 中的 permutations() 方法可用于轻松计算有替换和无替换的排列。它返回一个数据帧或一个矩阵,该矩阵在对受约束的指定向量的元素进行混洗时形成的可能排列。可以使用 nrow() 方法捕获此类可能排列的数量,该方法返回获得的输出数据帧中的行数。
Syntax:
permutations(n , r, vec, repeats.allowed=F)
Parameters :
- n – no. of objects to choose from
- r – no. of objects to choose
- vec – the atomic vector or matrix to shuffle
- repeats.allowed – By default : false. If true, the permutations are generated with repetition allowed
Return :
A data frame or matrix with plausible permutations. The number of rows in the data frame is equivalent to the value of 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