If I have two vectors X<-1:10 Y<-1:5 When I combine them using cbind, the shorter one is repeated and both are made of the same length. Is there a methods that does this without duplicating the shorter one. I want to use this to store the data back to a file. Thanks ../Murli [[alternative HTML version deleted]]
Something like this, perhaps: cbind( X, c(Y, rep(NA,length(X)-length(Y))) ) This just extends Y with NA instead of recycling Y. If you are going to cbind() the vectors must be the same length. You *must* have something there. -Don At 1:51 PM -0400 10/4/07, Nair, Murlidharan T wrote:>If I have two vectors >X<-1:10 >Y<-1:5 >When I combine them using cbind, the shorter one is repeated and >both are made of the same length. Is there a methods that does this >without duplicating the shorter one. I want to use this to store the >data back to a file. >Thanks ../Murli > > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
--- "Nair, Murlidharan T" <mnair at iusb.edu> wrote:> If I have two vectors > X<-1:10 > Y<-1:5 > When I combine them using cbind, the shorter one is > repeated and both are made of the same length. Is > there a methods that does this without duplicating > the shorter one. I want to use this to store the > data back to a file. > Thanks ../Murli >It's not quite clear what you want to do. If you just want to save an .Rdata file you could just put the variables in a list. X<-1:10 Y<-1:5 mylist <-list(X,Y) If you want to save to something like a data.frame to a csv file then you probably need to add some padding to the file. The function below adds NA's and turns the matrix into a data.frame padding <- function(a,b) { zz <-rep(NA,length(X)-length(Y)) YY <- c(Y,zz) out <- data.frame(cbind(a,YY)) } dd <- padding(X,Y)
Assuming they are numeric, cbind them as time series: as.data.frame(cbind(x = ts(1:10), y = ts(1:5))) On 10/4/07, Nair, Murlidharan T <mnair at iusb.edu> wrote:> If I have two vectors > X<-1:10 > Y<-1:5 > When I combine them using cbind, the shorter one is repeated and both are made of the same length. Is there a methods that does this without duplicating the shorter one. I want to use this to store the data back to a file. > Thanks ../Murli > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
Well, if you bind two vectors you form an array with dimensions 2 x length of the longest vector. So you need to decide how to fill up the 'empty' spacies corresponding to the shorter vector. Recycling the shorter vector is the default action. If you just want to save the data, you could create a list and save it as a R object. my.list=list(X,Y) save(my.list) Julian Nair, Murlidharan T wrote:> If I have two vectors > X<-1:10 > Y<-1:5 > When I combine them using cbind, the shorter one is repeated and both are made of the same length. Is there a methods that does this without duplicating the shorter one. I want to use this to store the data back to a file. > Thanks ../Murli > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Thanks to all for their suggestions!! Cheers../Murli -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: Thursday, October 04, 2007 3:30 PM To: Nair, Murlidharan T Cc: r-help at stat.math.ethz.ch Subject: Re: [R] combining vectors on unequal length Assuming they are numeric, cbind them as time series: as.data.frame(cbind(x = ts(1:10), y = ts(1:5))) On 10/4/07, Nair, Murlidharan T <mnair at iusb.edu> wrote:> If I have two vectors > X<-1:10 > Y<-1:5 > When I combine them using cbind, the shorter one is repeated and both are made of the same length. Is there a methods that does this without duplicating the shorter one. I want to use this to store the data back to a file. > Thanks ../Murli > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >