user3664020
user3664020

Reputation: 3020

keep x rows and remove rest by 2 columns

I have a data.table say, dat. Following is its dput

structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.4, 0.2, 0.1, 0.1), Species = c("a", "a", "a", "a", "b", 
"b", "b", "b", "b", "b")), .Names = c("Sepal.Length", "Sepal.Width", 
"Petal.Length", "Petal.Width", "Species"), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000330788>)


dat

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
 1:          5.1         3.5          1.4         0.2       a
 2:          4.9         3.0          1.4         0.2       a
 3:          4.7         3.2          1.3         0.2       a
 4:          4.6         3.1          1.5         0.2       a
 5:          5.0         3.6          1.4         0.2       b
 6:          5.4         3.9          1.7         0.4       b
 7:          4.6         3.4          1.4         0.4       b
 8:          5.0         3.4          1.5         0.2       b
 9:          4.4         2.9          1.4         0.1       b
10:          4.9         3.1          1.5         0.1       b

I want to do the following,

1) Calculate minimum frequency of any combination of columns Petal.Width and Species in the data (which is 2 in above example). Let us call this number as x. Following are the combinations of these 2 columns in the data

Species    Petal.Width  Frequency
a           0.2           4
b           0.2           2 
b           0.4           2
b           0.1           2    

2) Keep only x (2 in our example) observations at random for each combination of columns Petal.Width and Species

When we have to keep only 1 case at random in such a scenario we do

dat <- unique(dat, by = c("Petal.Width", "Species")

But what do we do when we want to keep x cases instead of just 1 (that too randomly)?

Output may look like following

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
 2:          4.9         3.0          1.4         0.2       a
 4:          4.6         3.1          1.5         0.2       a
 5:          5.0         3.6          1.4         0.2       b
 6:          5.4         3.9          1.7         0.4       b
 7:          4.6         3.4          1.4         0.4       b
 8:          5.0         3.4          1.5         0.2       b
 9:          4.4         2.9          1.4         0.1       b
10:          4.9         3.1          1.5         0.1       b

In this example, only 2 cases of combination (a, 0.2) will be removed since other combinations satisfy the minimum frequency criteria.

Upvotes: 0

Views: 52

Answers (1)

danas.zuokas
danas.zuokas

Reputation: 4643

First part:

x <- dat[, .N, by = list(Species, Petal.Width)][, min(N)]

Second part:

dat[, .SD[sample(1:.N, x)], by = list(Species, Petal.Width)]

Upvotes: 2

Related Questions