r mapply vs "bad lapply"
Asked Answered
S

1

6

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???

Soothe answered 5/1, 2018 at 3:27 Comment(2)
Just to know: purrr::map2(data, train, function(x, y) x[y, ]) works tooSoothe
purrr::map2(data, train, ~ .x[.y, ]) is a more purrr-y way or even ~.[.y, ]Sapiential
H
4

With mapply, the default option is SIMPLIFY = TRUE and it simplifies it to an array when the dimensions are the same. If we change it to FALSE, the output will be a list

mapply(function(x,y) x[y, ], data, train, SIMPLIFY = FALSE)

Or use the Map wrapper

Map(function(x, y) x[y, ], data, train)
Hengist answered 5/1, 2018 at 3:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.