user
user

Reputation: 234

Subset a dataset to leave the largest 2 values

I have a data set:

col1 col2 

A     3
A     3
B     2
C     1
B     2
A     3
D     5
B     2
D     5
B     2
F     0
F     0
A     3
C     1
C     1

How can I subset it so as to "leave" the top 2 col1 values. So my output is this:

col1 col2 

A     3
A     3
A     3
D     5
A     3

I have viewed this question, but it didn't answer my question.

Upvotes: 1

Views: 43

Answers (2)

Jonas
Jonas

Reputation: 1810

I assume that your data is in a data.frame.

First of all, you need to get the top 2 values of col2. Therefore you can take the unique values of it, sort them in decreasing order, and take the first two elements:

col2Values <- unique(df$col2)
top2Elements <- sort(col2Values,decreasing = TRUE)[c(1,2)]

Now you know the top2 values, so you just need to check where these values appear in col2. This can be done via:

df[df$col2 %in% top2Elements,]

Update: Now it should work, I had some typos in there.

Upvotes: 2

Duck
Duck

Reputation: 39595

Try this, but not sure why you only have one D:

#Code
newdf <- df[df$col2 %in% sort(unique(df$col2),decreasing = T)[1:2],]

Upvotes: 2

Related Questions