Serguei Kaniovski
2006-Oct-16 08:27 UTC
[R] Fill a matrix with vectors of different lengths
Hello, how can I best fill a (fixed) matrix with vectors of varying lengths by column, setting the superfluous elements to zero? Here is my code: v<-(1,1,2,3,4,6) binc<-function(x){ l<-sum(x)+1 y<-c(1,rep(0,l-1)) for (i in x) y<-y+c(rep(0,i),y)[1:l] } mat<-matrix(0, nrow=sum(v)+1, ncol=length(v)) for (i in 1:length(v)) mat[,i]=mat[,i]+binc(v[1:i]) the expression in the for-loop produces an error since mat[,i] and binc(v[1:i]) are of unequal lengths, except on the last iteration. Thanks in advance, Serguei -- ___________________________________________________________________ Austrian Institute of Economic Research (WIFO) Name: Serguei Kaniovski P.O.Box 91 Tel.: +43-1-7982601-231 Arsenal Objekt 20 Fax: +43-1-7989386 1103 Vienna, Austria Mail: Serguei.Kaniovski at wifo.ac.at http://www.wifo.ac.at/Serguei.Kaniovski
Dimitris Rizopoulos
2006-Oct-16 08:51 UTC
[R] Fill a matrix with vectors of different lengths
you can try something like the following: out <- lapply(1:length(v), function(i) binc(v[1:i])) nout <- max(sapply(out, length)) sapply(out, function(x) c(x, rep(0, nout - length(x)))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Serguei Kaniovski" <kaniovsk at wifo.ac.at> To: <r-help at stat.math.ethz.ch> Sent: Monday, October 16, 2006 10:27 AM Subject: [R] Fill a matrix with vectors of different lengths> Hello, > > how can I best fill a (fixed) matrix with vectors of varying lengths > by > column, setting the superfluous elements to zero? Here is my code: > > v<-(1,1,2,3,4,6) > > binc<-function(x){ > l<-sum(x)+1 > y<-c(1,rep(0,l-1)) > for (i in x) y<-y+c(rep(0,i),y)[1:l] > } > > mat<-matrix(0, nrow=sum(v)+1, ncol=length(v)) > for (i in 1:length(v)) mat[,i]=mat[,i]+binc(v[1:i]) > > the expression in the for-loop produces an error since mat[,i] and > binc(v[1:i]) are of unequal lengths, except on the last iteration. > > Thanks in advance, > Serguei > -- > ___________________________________________________________________ > > Austrian Institute of Economic Research (WIFO) > > Name: Serguei Kaniovski P.O.Box 91 > Tel.: +43-1-7982601-231 Arsenal Objekt 20 > Fax: +43-1-7989386 1103 Vienna, Austria > Mail: Serguei.Kaniovski at wifo.ac.at > > http://www.wifo.ac.at/Serguei.Kaniovski > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
for (i in 1:length(v)) { bi <- binc(v[1:i]) mat[1:length(bi),i]=mat[1:length(bi),i]+bi } On 16/10/06, Serguei Kaniovski <kaniovsk at wifo.ac.at> wrote:> Hello, > > how can I best fill a (fixed) matrix with vectors of varying lengths by > column, setting the superfluous elements to zero? Here is my code: > > v<-(1,1,2,3,4,6) > > binc<-function(x){ > l<-sum(x)+1 > y<-c(1,rep(0,l-1)) > for (i in x) y<-y+c(rep(0,i),y)[1:l] > } > > mat<-matrix(0, nrow=sum(v)+1, ncol=length(v)) > for (i in 1:length(v)) mat[,i]=mat[,i]+binc(v[1:i]) > > the expression in the for-loop produces an error since mat[,i] and > binc(v[1:i]) are of unequal lengths, except on the last iteration. > > Thanks in advance, > Serguei > -- > ___________________________________________________________________ > > Austrian Institute of Economic Research (WIFO) > > Name: Serguei Kaniovski P.O.Box 91 > Tel.: +43-1-7982601-231 Arsenal Objekt 20 > Fax: +43-1-7989386 1103 Vienna, Austria > Mail: Serguei.Kaniovski at wifo.ac.at > > http://www.wifo.ac.at/Serguei.Kaniovski > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP