Dear all, I am trying to apply the sapply function on a list, which comes out from a matrix of 25 rows and two columns, so that each element of the list, is a two column element. In my function within sapply, say f, I would like each element of the list to become a vector so that I can transpose it (in a way to let it be a column vector with two rows) and then perform a row*column multiplication. But actually it cannot work it out. Any suggestion for it? Many thanks alessia
turning an element of list into a vector
3 messages · alessia matano, David Winsemius, Jim Lemon
On Jan 16, 2010, at 12:05 PM, alessia matano wrote:
Dear all, I am trying to apply the sapply function on a list, which comes out from a matrix of 25 rows and two columns, so that each element of the list, is a two column element. In my function within sapply, say f, I would like each element of the list to become a vector so that I can transpose it (in a way to let it be a column vector with two rows) and then perform a row*column multiplication. But actually it cannot work it out. Any suggestion for it?
Yes.
and provide commented, minimal, self-contained, reproducible code.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ David Winsemius, MD Heritage Laboratories West Hartford, CT
1 day later
On 01/17/2010 04:05 AM, alessia matano wrote:
Dear all, I am trying to apply the sapply function on a list, which comes out from a matrix of 25 rows and two columns, so that each element of the list, is a two column element. In my function within sapply, say f, I would like each element of the list to become a vector so that I can transpose it (in a way to let it be a column vector with two rows) and then perform a row*column multiplication. But actually it cannot work it out.
Hi Alessia, Your description of the problem is a bit hard to understand. It seems that you want to make a list out of a 25x2 matrix (amat): amat.list<-list() for(i in 1:dim(amat)[1]) amat.list[[i]]<-amat[i,] Then you can sapply on the list: sapply(amat.list,function(x) x[1]*x[2]) but this seems a very roundabout way to get the row products of the matrix. Jim