[R] stacking consecutive columns

peter dalgaard pdalgd at gmail.com
Thu Nov 18 09:21:06 CET 2010


On Nov 17, 2010, at 16:37 , Graves, Gregory wrote:

> Follows is the exact solution to this:
> 
> v <- NULL
> 
> #note that decreasing is FALSE so preceding year preceeds
> 
> for(i in 2:46) {
>  kk <- melt(yearmonth[, c(1, i, i+1)], id.vars="month", variable_name="year")
>  v[[i-1]] <- kk[order(kk$year, decreasing=FALSE),]
> }
> 
> x <- do.call(cbind, v)
> write.table(x,"clipboard",sep="	",col.names=NA)  #export to Excell via Ctrl-V
> 

Just wondering, was anything wrong with

y1 <- cbind(yearmonth[,-1], foo = NA)
names(y1) <- names(yearmonth)
rbind(yearmonth, y1)

? (untested, of course)


-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list