paste(letters[1:5], as.character(1:5))
[1] "a 1" "b 2" "c 3" "d 4" "e 5"
> paste(letters[1:5], as.character(1:3))
[1] "a 1" "b 2" "c 3" "d 1" "e 2"
> expand.grid(pants = c("blue", "black"), shirt = c("white", "grey", "plaid")) # 모든 조합 생성?
pants shirt
1 blue white
2 black white
3 blue grey
4 black grey
5 blue plaid
6 black plaid
> install.packages("gtools")
> library(gtools)
> permutations(5,2)
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 1 5
[5,] 2 1
[6,] 2 3
[7,] 2 4
[8,] 2 5
[9,] 3 1
[10,] 3 2
[11,] 3 4
[12,] 3 5
[13,] 4 1
[14,] 4 2
[15,] 4 3
[16,] 4 5
[17,] 5 1
[18,] 5 2
[19,] 5 3
[20,] 5 4
> combinations(5,2)
[,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
> all_phone_numbers <- permutations(10, 7, v = 0:9)
> n <- nrow(all_phone_numbers)
> n
[1] 604800
> index <- sample(n, 5)
> index
[1] 124574 106782 415511 232305 465598
> all_phone_numbers[index,]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 2 0 6 4 1 8 5
[2,] 1 7 9 0 8 2 4
[3,] 6 8 7 4 3 2 5
[4,] 3 8 5 4 9 1 0
[5,] 7 6 2 1 9 8 3
> suits <- c("d", "c", "h", "s")
> numbers <- c("a", "j", "q", "k", as.character(1:10))
> deck <- expand.grid(number=numbers, suit=suits)
> deck<-paste(deck$number, deck$suit)
> deck
[1] "a d" "j d" "q d" "k d" "1 d" "2 d"
[7] "3 d" "4 d" "5 d" "6 d" "7 d" "8 d"
[13] "9 d" "10 d" "a c" "j c" "q c" "k c"
[19] "1 c" "2 c" "3 c" "4 c" "5 c" "6 c"
[25] "7 c" "8 c" "9 c" "10 c" "a h" "j h"
[31] "q h" "k h" "1 h" "2 h" "3 h" "4 h"
[37] "5 h" "6 h" "7 h" "8 h" "9 h" "10 h"
[43] "a s" "j s" "q s" "k s" "1 s" "2 s"
[49] "3 s" "4 s" "5 s" "6 s" "7 s" "8 s"
[55] "9 s" "10 s"
> kings<-paste("k", suits)
> kings
[1] "k d" "k c" "k h" "k s"
> mean(deck %in% kings)
[1] 0.07142857
> hands <- permutations(52, 2, v=deck) #deck 중에서 2개씩 뽑기
> nrows(hands)
[1] 2652
> first_card <- hands[,1] #first column
> second_card <- hands[,2]
> sum(first_card %in% kings)
[1] 204
> sum(first_card %in% kings & second_card%in% kings)/sum(first_card %in% kings)
[1] 0.05882353
> combinations(3,2)
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 2 3
'programing > R studio' 카테고리의 다른 글
[Rstudio] rep, replicate, sample, table, prop.table (0) | 2021.06.28 |
---|---|
R 패키지 설치 에러 (3) | 2019.06.09 |
R 단축키 (0) | 2019.03.23 |
시계열 ts() (0) | 2019.01.30 |
approx VS approxfun (0) | 2019.01.30 |