Hi devel list, I searched for past posts about this issue and found none, so my apologies if this has been addressed before. The sapply() function has an argument 'simplify', and the mapply() function has an argument 'SIMPLIFY'. I am surprised that the apply() argument does not have a similar argument. Is there a reason for this? Here's a simple example:> x = matrix(1:12, 3, 4) > apply(x, 1, sum)[1] 22 26 30 This is what I would like to see:> apply(x, 1, sum, simplify = FALSE)[[1]] [1] 22 [[2]] [1] 26 [[3]] [1] 30 Looking at the function definition of apply(), I imagine it wouldn't be too hard to add such an argument. Add an argument 'simplify' with default value TRUE, and include the following line in the function definition. if(simplify == FALSE) return (ans) Is this a good idea? Would this be a good addition to R's apply() function? Are there stability issues in making this change (for example, what if some previous code threw a 'simplify' argument into the '...')? Best, -David Johnston -- View this message in context: http://r.789695.n4.nabble.com/apply-returning-a-list-tp3679619p3679619.html Sent from the R devel mailing list archive at Nabble.com.
On Jul 19, 2011, at 7:34 PM, David A. Johnston wrote:> Hi devel list, > > I searched for past posts about this issue and found none, so my > apologies > if this has been addressed before. > > The sapply() function has an argument 'simplify', and the mapply() > function > has an argument 'SIMPLIFY'. I am surprised that the apply() > argument does > not have a similar argument. Is there a reason for this? > > Here's a simple example: >> x = matrix(1:12, 3, 4) >> apply(x, 1, sum) > [1] 22 26 30 > > This is what I would like to see: >> apply(x, 1, sum, simplify = FALSE) > [[1]] > [1] 22 > > [[2]] > [1] 26 > > [[3]] > [1] 30 >Probably no one thought it was that difficult to type the more concise and equally expressive: > as.list(apply(x, 1, sum)) [[1]] [1] 22 [[2]] [1] 26 [[3]] [1] 30 -- David Winsemius, MD West Hartford, CT