1

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.

user
  • 235
  • 2
  • 16

2 Answers2

2

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],]
Duck
  • 39,058
  • 13
  • 42
  • 84
2

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.

Jonas
  • 1,760
  • 1
  • 3
  • 12