rlistdataframeextractremove-if

how to extract rows in dataframe based on another list


I have cleaned up one of the columns in a set of data boxes and generated a list, then I now want to extract the entire data box that matches the cleaned up data (list).

For example, I have a cleaned list:

x =  c(1,2,3,4,2,1,3)

and my original dataframe is:

> b <- data.frame( y =  c(1,2,3,4,5,6,2,1,3,5,2,1), 
+                  z = c("a","s","d","f","g","h","j","k","l","z","x","c"))
> b
   y z
1  1 a
2  2 s
3  3 d
4  4 f
5  5 g
6  6 h
7  2 j
8  1 k
9  3 l
10 5 z
11 2 x
12 1 c

I want to have my updated dataframe looks like:

  y z
1 1 a
2 2 s
3 3 d
4 4 f
5 2 j
6 1 k
7 3 l

Then I used:

> b1<-subset(b, (y %in% x))
> b1
   y z
1  1 a
2  2 s
3  3 d
4  4 f
7  2 j
8  1 k
9  3 l
11 2 x
12 1 c

But my list does not contain

11 2 x
12 1 c

What do I have to do to get the data frame I want? (order matter)


Solution

  • Count the number of times each number appears in x with table(x), and, for each group/value in y, remove rows exceeding that value:

    b %>%
      group_by(y) %>%
      filter(row_number() <= table(x)[paste(cur_group())])