簡體   English   中英

如何 select n 行,例如一個給定列的值都是不同的?

[英]How to select n rows such as the values of one given column are all distinct?

下午好!

假設我有以下矩陣:

 Sensor location  Target location detection Probability
1                 7              13             0.2943036
2                21              15             0.2943036
3                16              13             0.2943036
4                18              15             0.2943036
5                21              15             0.2943036
6                 1               2             0.2943036
7                16              22             0.2943036
8                10               4             0.2943036
9                16              17             0.2943036
10                2               5             0.2943036
11               13              16             0.2943036
12                9              12             0.2943036
13                2               8             0.2943036
14                7               1             0.2943036
15                7              10             0.2943036
16                1               2             0.2943036
17               18              12             0.2943036
18               23              17             0.2943036
19               21              15             0.2943036
20               20              21             0.2943036
21                2               1             0.2943036
22               12              18             0.2943036
23               24              21             0.2943036
24               22              23             0.2943036
25                2               3             0.2943036
26               11              10             0.2943036
27                7              10             0.2943036
28                2               3             0.2943036
29               12               6             0.2943036
30                2               1             0.2943036
31               24              21             0.2943036
32               14               8             0.2943036

如何從這個矩陣中采樣 n 行,例如第二列的值都是不同的

所需 output 的示例( Target location列值必須是唯一的):

n = 4:

         Sensor location  Target location detection Probability 
4                18              15             0.2943036
7                16              22             0.2943036
8                10               4             0.2943036
9                16              17             0.2943036

不需要的 output (值15在第二列中出現多次):

      Sensor location  Target location detection Probability 
4                18              15             0.2943036
2                21              15             0.2943036
8                10               4             0.2943036
9                16              17             0.2943036

我知道dplyr具有sample_n()dplyr::distinct類的功能,我嘗試過:

data %>% distinct("Target location")

我希望我的問題很清楚,非常感謝您的幫助!

您可以執行以下操作:

n <- 25
indicesToSampleFrom <- which(!duplicated(data[["Target location"]]))
data[sample(indicesToSampleFrom,n),]

編輯:如果您想將此邏輯應用於其他列,最好檢查是否有足夠的不同值。 因此,不是采樣n行,而是采樣min(n,length(indicesToSampleFrom))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM