Re: [R] Matrix interesting question!

2010-05-29 Thread Jim Lemon

On 05/29/2010 02:30 AM, UM wrote:


hi,
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
ages)


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?



Hi UM,
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.


dfapply2<-function(x,FUN,args) {
 dimx<-dim(x)
 newx<-list()
 for(column in 1:dimx[2])
  newx[[column]]<-do.call(FUN,list(x[column],args[column]))
 names(newx)<-names(x)
 return(as.data.frame(newx))
}

Pretty rough, but it does apply the arguments in "args" to the 
respective columns. So, thanks for motivating me to program this.


Jim

digahole<-function(x) {
 dimx<-dim(x)
 years<-as.numeric(colnames(x))
 ages<-as.numeric(rownames(x))
 minby<-min(years)-max(ages)
 maxby<-max(years)-min(ages)
 newx<-matrix(NA,nrow=dimx[1],ncol=1+maxby-minby)
 rownames(newx)<-rownames(x)
 colnames(newx)<-minby:maxby
 oldrow<-rep(1:dimx[1],each=dimx[2])
 oldcol<-rep(1:dimx[2],dimx[1])
 newcol<-oldcol+rep(max(ages)-ages,each=dimx[1])
 for(element in 1:length(oldrow))
  newx[oldrow[element],newcol[element]]<-x[oldrow[element],oldcol[element]]
 return(newx)
}

fillitup<-function(x) {
 dimx<-dim(x)
 byears<-as.numeric(colnames(x))
 ages<-as.numeric(rownames(x))
 minyr<-min(byears)+max(ages)
 maxyr<-max(byears)+min(ages)
 oldx<-matrix(NA,nrow=dimx[1],ncol=1+maxyr-minyr)
 rownames<-rownames(x)
 colnames<-minyr:maxyr
 for(row in 1:dimx[1]) oldx[row,]<-x[row,which(!is.na(x[row,]))]
 return(oldx)
}

fillitup(digahole(A))

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix interesting question!

2010-05-28 Thread David Winsemius


On May 28, 2010, at 12:30 PM, UM wrote:



hi,
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

ages)


I would like to first turn matrix A into matrix B



> catrow <- function(A, rn) c(rep("0", 3-rn), A[rn,],rep("0", 
(2+rn)-3) )
> matrix(sapply(1:3, function(x) catrow(A, x)) , ncol=2*ncol(A)-1,  
byrow=TRUE)

 [,1] [,2] [,3] [,4] [,5]
[1,] "0"  "0"  "a"  "b"  "c"
[2,] "0"  "d"  "e"  "f"  "0"
[3,] "g"  "h"  "i"  "0"  "0"

And then I would like to convert matrix B back again to the original  
matrix

A.


Left as an exercise for the reader.

(I have left out details of steps) but this is the gist of what I  
want to

do. Can anyone please give any insights?



David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix interesting question!

2010-05-28 Thread Joshua Wiley
Hi,

Can you provide sample data?  It seems like in both matrices, you have
ages in the rows.  Do you just want to calculate birth years in matrix
B from ages and years in matrix A?

It may also help to give us some of the details of what you are doing
once you have transformed it prior to transforming back (e.g., are
there changes that would impact what the code to convert back needs to
be?).

Josh


On Fri, May 28, 2010 at 9:30 AM, UM  wrote:
>
> hi,
> 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
> ages)
>
>
> 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?
>
>
> Thanks
>
>
>
>
>
>
>
>
>
> http://r.789695.n4.nabble.com/file/n2234852/untitled.bmp
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Matrix-interesting-question-tp2234852p2234852.html
> Sent from the R help mailing list archive at Nabble.com.
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix interesting question!

2010-05-28 Thread Joris Meys
Provide a minimal example to start with. This sounds more like voodoo than
anything else.
Cheers
Joris

On Fri, May 28, 2010 at 6:30 PM, UM  wrote:

>
> hi,
> 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
> ages)
>
>
> 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?
>
>
> Thanks
>
>
>
>
>
>
>
>
>
> http://r.789695.n4.nabble.com/file/n2234852/untitled.bmp
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Matrix-interesting-question-tp2234852p2234852.html
> Sent from the R help mailing list archive at Nabble.com.
>
>[[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joris Meys
Statistical Consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

Coupure Links 653
B-9000 Gent

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Matrix interesting question!

2010-05-28 Thread UM

hi,
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
ages)


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?


Thanks
 

 
   
   
 
  
 
 
http://r.789695.n4.nabble.com/file/n2234852/untitled.bmp 

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Matrix-interesting-question-tp2234852p2234852.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.