s-news
[Top] [All Lists]

Re: stack all columns

To: s-news@wubios.wustl.edu
Subject: Re: stack all columns
From: Todd Hatfield <hatfield@solander.bc.ca>
Date: Thu, 15 Nov 2001 22:04:14 -0800

Sincere thanks to Nick Ellis for his blisteringly fast response to my query. His "reshape" function (pasted below) does exactly what I want.

Todd Hatfield


reshape(air,,1:4)
where
reshape <-
function(obj, col.copy = NULL, col.unfold = 1:dim(obj)[2], label = "label",
value = "value")
{
n <- dim(obj)[1]
p <- length(col.unfold)
res <- vector("list", length(col.copy) + 2)
names(res) <- c(if(is.null(col.copy)) NULL else dimnames(obj[,
col.copy, drop = F])[[2]], label, value)
for(i in seq(along = col.copy)) {
res[[i]] <- rep(obj[, col.copy[i]], p)
}
res[[label]] <- rep(dimnames(obj[, col.unfold, drop = F])[[2]],
rep(n, p))
res[[value]] <- as.vector(data.matrix(obj[, col.unfold, drop = F]))
as.data.frame(res)
}


<Prev in Thread] Current Thread [Next in Thread>
  • Re: stack all columns, Todd Hatfield <=