1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Basic Commands and Statistics with R - 3- permutations # path: ~/ownCloud/STA_Statistics/Basic_Statistics/ # file_name: statistic_basics3.R # files_used: #install.packages( 'gtools' ) library( gtools ) #install.packages( "combinat" ) library( combinat ) #install.packages( "magrittr" ) library( "magrittr" ) # - - - - - - - - - - - - - - - - # x <- 6 factorial( x ) # [1] 720 for( i in 1:x ) print( factorial( i ) ) # [1] 1 # [1] 2 # [1] 6 # [1] 24 # [1] 120 # [1] 720 # - - - - - - - - - - - - - - - - # x <- c( "a", "b", "c" ) combinat::permn( x ) # [[1]] # [1] "a" "b" "c" # [[2]] # [1] "a" "c" "b" # [[3]] # [1] "c" "a" "b" # [[4]] # [1] "c" "b" "a" # [[5]] # [1] "b" "c" "a" # [[6]] # [1] "b" "a" "c" # - - - - - - - - - - - - - - - - # string conversion using combinat:: x <- "ABCDEFG" x <- strsplit( x, "" )[[1]] y <- combinat::permn( x ) y x <- LETTERS[1:5] y <- combinat::permn( x ) paste( y[[1]], collapse = "" ) for( i in 1:length( y ) ) print( paste( y[[i]], collapse = "" ) ) # - - - - - - - - - - - - - - - - # using gtools:: for permutations gtools::permutations( 5, 5, x ) # [,1] [,2] [,3] [,4] [,5] # [1,] "A" "C" "I" "M" "P" # [2,] "A" "C" "I" "P" "M" # [3,] "A" "C" "M" "I" "P" # [4,] "A" "C" "M" "P" "I" # [5,] "A" "C" "P" "I" "M" # ... gtools::permutations( 5, 5, x, repeats.allowed = TRUE ) # [,1] [,2] [,3] [,4] [,5] # [1,] "A" "A" "A" "A" "A" # [2,] "A" "A" "A" "A" "C" # [3,] "A" "A" "A" "A" "I" # [4,] "A" "A" "A" "A" "M" # [5,] "A" "A" "A" "A" "P" # [6,] "A" "A" "A" "C" "A" # [7,] "A" "A" "A" "C" "C" # [8,] "A" "A" "A" "C" "I" # [9,] "A" "A" "A" "C" "M" # [10,] "A" "A" "A" "C" "P" # ... # - - - - - - - - - - - - - - - - # using gtools:: for combinations gtools::combinations( 4, 2, x ) gtools::combinations( 5, 2, x ) gtools::combinations( 5, 3, x ) gtools::combinations( 4, 2, x, repeats.allowed = TRUE ) gtools::combinations( 2, 5, x, repeats.allowed = TRUE ) # Martin Stoppacher # # office@martinstoppacher.com # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ################################################################################# |
Basic R commands – 3- permutations
Leave a reply