Is it possible to deactivate the as.matrix() conversion of apply()?
In the R documentation and in previous posts at stack overflow, I could not find any flags to solve this problem.
Example: Selection of multiple sub matrices from a matrix using apply().
Problem: The apply() function automatically converts the results to a matrix. This leads to one big matrix containing all results.
Code:
#mat contains the data, m the desired column selections
mat <- matrix(c(1,2,3,4,
2,3,4,1,
2,4,3,1,
3,4,2,1)
,nrow = 4)
colnames(mat) <- c(1,2,3,4)
m <- matrix(c(1,2,
3,4)
,nrow = 2)
#Selects first 2 and last 2 columns of mat
#Returns matrix of both results (connected with rbind)
#instead of list of 2 matrices
l <- apply(m,1,function(r)mat[,r])
It is clear that the workaround for this example is simple (select rows manually), but I am trying to write generic code for bigger data sets.
apply(m, 1, function(r) list(mat[,r]))
help? - csgillespiel[[n]][[1]]
where n is 1 or 2 according to this example. - Tensibai