[R] Create data frame from header only
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Aug 5 05:41:40 CEST 2008
On Mon, 4 Aug 2008, Etienne B. Racine wrote:
>
> Even if it's not so elegant, I will bind it in a function, so I don't have to
> bother with that anymore. I think there should be a simple function in R to
> initialize an empty data frame. From what I've read, it is a recurrent
> question on that list.
Not from what I have seen. But it is easy with data.frame
colname.list <- paste("A",1:5,sep="") # note this is not a list.
cols <- lapply(colname.list, function(x) numeric(0))
names(cols) <- colname.list
do.call("data.frame", cols)
>
> #Create an empty data frame from a header list
> empty.df<- function(header){
> df<-data.frame(matrix(matrix(rep(1,length(header)),1),1))
> colnames(df)<-header
> return(df[NULL,])
> }
>
> # Usage
> new.df <- empty.df(paste("A",1:5,sep=""))
>> new.df
> [1] A1 A2 A3 A4 A5
> <0 rows> (or 0-length row.names)
>
> Thanks again,
> Etienne
>
>
> milton ruser wrote:
>>
>> Hi Etienne
>>
>> It is not so elegant, bu I think that work.
>>
>>
>> colname.list<-paste("A",1:5,sep="")
>>
>> df<-data.frame(matrix(matrix(rep(1,length(colname.list)),1),1))
>> df
>> colnames(df)<-colname.list
>> df
>>
>> df<-df[-1,]
>> df
>>
>> Cheers,
>>
>>
>> Miltinho Astronauta
>> Brazil
>>
>
> --
> View this message in context: http://www.nabble.com/Create-data-frame-from-header-only-tp18822575p18823082.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list