Dear R-Users I have a list with two vectors of doubles tha have different lengths. I want to export it to a file and I also want to print it in two columns. I try with write.table but it need vectors of the same length. Does anyone know how to do it? Thanks Borja [[alternative HTML version deleted]]
Hi Borja, write.table doesn't work, because it's not clear how to print it in two columns, so we can't help you yet either. Do you want to match the first item in v1 with the first item in v2, and have a bunch of NAs hanging off the end of the shorter column in the output? Or align the last items? Or do you want to just drop the "extra" items in one column? Etc. On Sat, 20 Dec 2008, Borja Soto Varela wrote:> Dear R-Users > > I have a list with two vectors of doubles tha have different lengths. I want > to export it to a file and I also want to print it in two columns. > I try with write.table but it need vectors of the same length. > > Does anyone know how to do it? > > > Thanks > Borja > > [[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. >
Perhaps not the most elegant solution, but this seems to "work".
> a<- rnorm(5)
> b <- rnorm(10)
> clis <- list(a,b)
> dput(clis, "testfile") # writes to file
#dput works for arbitrary structures
Now pad the shorter of the vectors with NA's and "print" the
result of
cbind
> mv <-max(length(clis[[1]]),length(clis[[2]]))
> cbind( c(clis[[1]], rep(NA, mv - length(clis[[1]]) )),
c(clis[[2]], rep(NA, mv - length(clis[[2]]) )) )
[,1] [,2]
[1,] -0.561558774 0.44756791
[2,] -1.226817284 -0.24501794
[3,] -0.333583965 -0.07643339
[4,] 0.006174515 1.12304951
[5,] 0.246559481 1.58075036
[6,] NA -0.82786133
[7,] NA 0.49121165
[8,] NA 0.01857698
[9,] NA 1.32094819
[10,] NA 0.93023074
--
David Winsemius
On Dec 20, 2008, at 2:41 PM, Borja Soto Varela wrote:
> Dear R-Users
>
> I have a list with two vectors of doubles tha have different
> lengths. I want
> to export it to a file and I also want to print it in two columns.
> I try with write.table but it need vectors of the same length.
>
> Does anyone know how to do it?
>
>
> Thanks
> Borja
>
> [[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.