Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r mapply vs "bad lapply"

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

like image 577
Giovanni Iacopetti Avatar asked Jan 05 '18 03:01

Giovanni Iacopetti


1 Answers

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)
like image 84
akrun Avatar answered Nov 04 '22 05:11

akrun