[R] idiom for constructing data frame
Ista Zahn
istazahn at gmail.com
Tue Mar 31 20:50:12 CEST 2015
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.
More information about the R-help
mailing list