-2

I want to find all the possible samples of two classes from the 5 different classes. I am trying this:

x <- c(A = 23, B = 21, C = 29, D = 17, E = 18)

mean (x)

sd (x)

sample(x)

sample(x, 2, replace = FALSE, prob = NULL)

sample(x, 5, replace = FALSE, prob = NULL)

but I think it is wrong it does not give me the number of all the possible samples

David Arenburg
  • 91,361
  • 17
  • 137
  • 196

3 Answers3

2

Try this

install.packages("gtools")
library(gtools)
permutations(length(x), 2, x)
#       [,1] [,2]
#  [1,]   17   18
#  [2,]   17   21
#  [3,]   17   23
#  [4,]   17   29
#  [5,]   18   17
#  [6,]   18   21
#  [7,]   18   23
#  [8,]   18   29
#  [9,]   21   17
# [10,]   21   18
# [11,]   21   23
# [12,]   21   29
# [13,]   23   17
# [14,]   23   18
# [15,]   23   21
# [16,]   23   29
# [17,]   29   17
# [18,]   29   18
# [19,]   29   21
# [20,]   29   23

If you just want to know the amount of possible permutations go with @Floo0s answer or do something like

nrow(permutations(length(x), 2, x))
# [1] 20
Community
  • 1
  • 1
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

If you have 5 classes. There are 5 over 2 possibilities.

see http://en.wikipedia.org/wiki/Binomial_coefficient

In R this is what the function choose calculates.

So

choose(length(x),2)

Answer: 10 possible combinations (if the order does not matter, so (A,B) is equal to (B,A))

If the order matters it is

choose(length(x),2) * factorial(2)

Answer: 20

As Colonel Beauvel mentioned. To find all these combinations use combn

Rentrop
  • 20,979
  • 10
  • 72
  • 100
1

Maybe by using:

combn(x, 2)

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   23   23   23   23   21   21   21   29   29    17
[2,]   21   29   17   18   29   17   18   17   18    18

Columns of the matrix will give you all possible combinations ignoring order.

Or use package combinat.

Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87