[R] Matrix interesting question!
jim at bitwrit.com.au
Sat May 29 09:15:45 CEST 2010
On 05/29/2010 02:30 AM, UM wrote:
> I have been trying to do this in R (have implemented it in Excel) but I have
> been using a very inefficent way (loops etc.). I have matrix A (columns are
> years and ages are rows) and matrix B (columns are birth yrs and rows are
> I would like to first turn matrix A into matrix B
> And then I would like to convert matrix B back again to the original matrix
> A. (I have left out details of steps) but this is the gist of what I want to
> do. Can anyone please give any insights?
The answer is somewhat trivial (see below) but my initial
misunderstanding of the question led me to write a function that I have
often wanted. When using an "apply" family function, I sometimes want to
apply a different argument to each slice of the object. I'm pretty sure
that this has been done before, and I even looked through the "plyr"
package but couldn't find what I wanted. Here's an example for a data frame.
for(column in 1:dimx)
Pretty rough, but it does apply the arguments in "args" to the
respective columns. So, thanks for motivating me to program this.
for(element in 1:length(oldrow))
for(row in 1:dimx) oldx[row,]<-x[row,which(!is.na(x[row,]))]
More information about the R-help