On 31/03/2015 1:52 PM, Sarah Goslee wrote:> I just snagged this from Duncan Murdoch's reply to the same question: > > # Create an empty dataframe to hold the results > df <- data.frame(strat=NA, id=NA, pid=NA)[rep(1, length(sel)),] > > This skips matrix(), but how to set the column names programmatically > within a function? > > Sarah, still sure I'm missing something obviousThe matrix() function has a dimnames argument, so you could do this: names <- c("strat", "id", "pid") data.frame(matrix(NA, nrow=10, ncol=3, dimnames=list(NULL, names))) Duncan Murdoch> > > On Tue, Mar 31, 2015 at 1:46 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote: > > Hi folks, > > > > I KNOW there has to be a way to do this more elegantly, but I > > consistently fail to come up with it, as I was just reminded while > > writing an example for a query on this list. > > > > What's a nifty way to construct a data frame of a given size? The only > > way I know of it to use matrix(), eg > > > > data.frame(matrix(NA, nrow=10, ncol=3)) > > > > and then to set the colnames in a second step. > > > > This comes up a lot when pre-allocated a data frame before using a > > loop: I know the size and column names, but want an empty structure to > > fill later. > > > > Sarah > > >
Hi, Duncan Murdoch suggested:> The matrix() function has a dimnames argument, so you could do this: > > names <- c("strat", "id", "pid") > data.frame(matrix(NA, nrow=10, ncol=3, dimnames=list(NULL, names)))That's a definite improvement, thanks. But no way to skip matrix()? It just seems unRlike, although since it's only full of NA values there are no coercion issues with column types or anything, so it doesn't hurt. It's just inelegant. :) Sarah -- Sarah Goslee http://www.functionaldiversity.org
You can make it as elegant as you want, e.g., make.empty.df <- function(nrow,ncol, names) { if(length(names) %% ncol != 0) stop("Lenght of names is not a multiple of the number of colums") data.frame(matrix(NA, nrow, ncol, dimnames = list(NULL, names))) } Best, Ista On Tue, Mar 31, 2015 at 2:37 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Hi, > > Duncan Murdoch suggested: > >> The matrix() function has a dimnames argument, so you could do this: >> >> names <- c("strat", "id", "pid") >> data.frame(matrix(NA, nrow=10, ncol=3, dimnames=list(NULL, names))) > > That's a definite improvement, thanks. But no way to skip matrix()? It > just seems unRlike, although since it's only full of NA values there > are no coercion issues with column types or anything, so it doesn't > hurt. It's just inelegant. :) > > Sarah > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
You can use structure() to attach the names to a list that is input to data.frame. E.g., dfNames <- c("First", "Second Name") data.frame(lapply(structure(dfNames, names=dfNames), function(name)rep(NA_real_, 5))) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Mar 31, 2015 at 11:37 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Hi, > > Duncan Murdoch suggested: > > > The matrix() function has a dimnames argument, so you could do this: > > > > names <- c("strat", "id", "pid") > > data.frame(matrix(NA, nrow=10, ncol=3, dimnames=list(NULL, names))) > > That's a definite improvement, thanks. But no way to skip matrix()? It > just seems unRlike, although since it's only full of NA values there > are no coercion issues with column types or anything, so it doesn't > hurt. It's just inelegant. :) > > Sarah > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]