[R] combining vectors on unequal length

John Kane jrkrideau at yahoo.ca
Thu Oct 4 20:40:49 CEST 2007


--- "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)



More information about the R-help mailing list