Q:

R how to subset a list use another list

I have two lists, and I want to subset listA by using listB.
Let's say I have listA and ListB, I want listC.
listA <- list(a = data.frame(x = 1:5, y = 6:10), 
           b = data.frame(x = 4:8, y = 7:11))

> listA
$a
  x  y
1 1  6
2 2  7
3 3  8
4 4  9
5 5  10
$b
  x  y
1 4  7
2 5  8
3 6  9
4 7  10
5 8  11

listB <- list(a = c(3,5), b = c(4, 7))

I want listC should be:
> listC
$a
  x  y
3 3  8
4 4  9
5 5  10
$b
  x  y
1 4  7
2 5  8
3 6  9
4 7  10

I appreciate any help!

A:

It sounds like you need to use mapply. Try this:
fun <- function(df, sq) df[df$x %in% seq(sq[1], sq[2]), ]
listC <- mapply(fun, listA, listB, SIMPLIFY = FALSE)
listC

This gives 
> listC
$a
  x  y
3 3  8
4 4  9
5 5 10

$b
  x  y
1 4  7
2 5  8
3 6  9
4 7 10

