I think I've missed something simple here: I have a list of data.frames, and a list of row numbers to select. Something like this:
a <- data.frame(q = c(1,0,0,0),
w = c(1,1,0,0),
e = c(1,1,1,0),
r = c(1,1,1,1))
b <- a + 1
c <- a + 2
d <- a + 3
data <- list(a = a, b = b, c = c, d = d)
ind_a <- c(1, 2)
ind_b <- c(1, 3)
ind_c <- c(1, 4)
ind_d <- c(2, 4)
train <- list(ind_a, ind_b, ind_c, ind_d)
now, I'd like to select the rows, and I thought that the proper form could be
test1 <- mapply(function(x,y) x[y, ], data, train)
but the only way I can make it work is
test2 <- lapply(1:4, function(x) data[[x]][train[[x]], ])
that seems like a fake for-loop to me...
Where I'm wrong???
purrr::map2(data, train, function(x, y) x[y, ])
works too – Soothepurrr::map2(data, train, ~ .x[.y, ])
is a more purrr-y way or even~.[.y, ]
– Sapiential