[R] How to cbind DF:s with differing number of rows?

Moshe Olshansky m_olshansky at yahoo.com
Mon Dec 3 05:39:36 CET 2007


Hi Lauri,

I see two possibilities.

Let say that you have
a <-c(1:5)
b <- c(1:7)
c <- c(1:4)
l <- list(a,b,c)

and you want to create an Excel file with columnA (1)
containing a (5 rows), column B (2) containing b and
column C containing c.

One possibility would be to write each ROW of the
output file separately (7 rows in your case) writing
nothing (just \t ) when you reach the end of the
particular item in the list. 

Another possibility would be to use xlsReadWritePro
package which allows to write each item (a,b,c) (or
list element - l[[1]],l[[2]],... etc) to a given
column of the output file.

Regards,

Moshe.

--- Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:

> #Hi R-users,
> #Suppose that I have a data.frame like this:
> 
> y1 <- rnorm(10) + 6.8
> y2 <- rnorm(10) + (1:10*1.7 + 1)
> y3 <- rnorm(10) + (1:10*6.7 + 3.7)
> y <- c(y1,y2,y3)
> x <- rep(1:3,10)
> f <- gl(2,15, labels=paste("lev", 1:2, sep=""))
> g <- seq(as.Date("2000/1/1"), by="day", length=30)
> DF <- data.frame(x=x,y=y, f=f, g=g)
> DF$g[DF$x == 1] <- NA
> DF$x[3:6] <- NA
> DF$wdays <- weekdays(DF$g)
> 
> DF
> 
> #For EDA purposes, I would like to calculate
> frequences in each variable
> g <- lapply(DF, function(x) as.data.frame(table(x)))
> 
> #After this, I would like to cbind these data.frames
> (in g) into a
> single data.frame (which to export to MS Excel)
> #do.call(cbind, g) does not seem to work because of
> the different
> number of rows in each data.frame.
> #The resulting data.frame shoul look like this (only
> two variables
> printed here):
> 
> Rowid;x;Freq.x;y;Freq.y; # etc...
> 1;1;9;1.69151845313816;1;
> 2;2;9;5.03748767699799;1;
> 3;3;8;5.37387749444247;1;
> 4;Empty;Empty;6.83926626214299;1;
> 5;Empty;Empty;6.97484558968873;1;
> 6;Empty;Empty;7.11023821708323;1;
> 7;Empty;Empty;7.1348316549091;1;
> 8;Empty;Empty;7.16727166992407;1;
> 9;Empty;Empty;7.35983428577469;1;
> 10;Empty;Empty;7.7596470136235;1;
> 11;Empty;Empty;7.86369414967578;1;
> 12;Empty;Empty;7.97164674771006;1;
> 13;Empty;Empty;8.0787295301318;1;
> 14;Empty;Empty;8.14161030348166;1;
> 15;Empty;Empty;8.20134832959661;1;
> 16;Empty;Empty;10.1469115339016;1
> 17;Empty;Empty;12.7442067301746;1
> 18;Empty;Empty;14.0865167751202;1
> 19;Empty;Empty;15.8280312307450;1
> 20;Empty;Empty;16.0484499360756;1
> 21;Empty;Empty;17.0795222149999;1
> 22;Empty;Empty;18.1254057823357;1
> 23;Empty;Empty;22.7169729331525;1
> 24;Empty;Empty;30.7237748005358;1
> 25;Empty;Empty;37.2141271786934;1
> 26;Empty;Empty;44.4954633229803;1
> 27;Empty;Empty;50.2302409305761;1
> 28;Empty;Empty;57.8913405112114;1
> 29;Empty;Empty;64.849897477945;1
> 30;Empty;Empty;71.4205263353053;1
> 
> 
> #Anyone have an idea how to do this?
> 
> #Thanks,
> #Lauri
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list