0
votes

I have a data frame (2 columns, some rows) and a function that takes a vector of 2 numbers and builds a matrix out of it. My goal is to apply that function on each row of the data frame such that in the end I have a list of matrices. I tried the following

df <- data.frame(c(1, 2, 3), c(4, 5, 6))
gen_mat <- function(x) matrix(c(x[1], x[1] + 1, x[2], x[2] +1), nrow = 2)
bad <- apply(df, 1, gen_mat)

Unfortunately, bad is not a list but a 4 by 3 matrix where each column contains the elements of one of the 3 generated matrices.

1

1 Answers

1
votes

Is this it?

bad2 <- lapply(as.data.frame(t(df)), gen_mat)
bad2