Suppose I am generating a matrix of card, each has its own letter (A/B/C) and its own number (2-11).
Now I randomly sampled 5 cards.
card <- data.frame(
pack = rep(c("A","B","C"), 10),
rank = rep(2:11, 3)
)
card
pack rank
1 A 2
2 B 3
3 C 4
4 A 5
5 B 6
6 C 7
7 A 8
8 B 9
9 C 10
10 A 11
11 B 2
12 C 3
13 A 4
14 B 5
15 C 6
16 A 7
17 B 8
18 C 9
19 A 10
20 B 11
21 C 2
22 A 3
23 B 4
24 C 5
25 A 6
26 B 7
27 C 8
28 A 9
29 B 10
30 C 11
card[sample(seq_len(nrow(card)), 5),]
And then our sample is generated.
Now I have a question. Suppose I am generating a logical value, so that:
"TRUE when the pack letter of all the elements in the sample is the same. FALSE otherwise."
I have a heuristic idea about such one-line code:
I select the pack letter column in the data frame, then use the unique function to remove duplicate letters, and measure the number of entries of the removed-repetition column. If such number is one, then TRUE, FALSE otherwise.
But I forget how to select the pack letter column in the data frame and measure the number of entries of the removed-repetition column. Could anyone help?
I apologize for the lengthy passage.
CodePudding user response:
How about this?
length(unique(card[sample(seq_len(nrow(card)), 5),][,1])) == 1
#------------------------
a<-c()
for(i in 1:1000){
a<-c(a,length(unique(card[sample(seq_len(nrow(card)), 5),][,1])) == 1)
}