Hi there, couple of questions on data frames: 1) Is there a way to build an empty data frame, containing nothing but the data frame variable names? 2) Is there a way to reorder the variables in a data frame, e.g. When I go to write out a data frame using write.table or write.matrix, I want the output in a certain order... 3) How to I "append" to the bottom of a dataframe? Thanks! --j -- Jonathan A. Greenberg, PhD NRC Research Associate NASA Ames Research Center MS 242-4 Moffett Field, CA 94035-1000 Office: 650-604-5896 Cell: 415-794-5043 AIM: jgrn307 MSN: jgrn307 at hotmail.com
>1) Is there a way to build an empty data frame, containing nothing but the >data frame variable names?Yes, you can do it one way as followings: df1<-as.data.frame(matrix(nrow=2,ncol=2),row.names=c("R1","R2"))>2) Is there a way to reorder the variables in a data frame, e.g. When I go >to write out a data frame using write.table or write.matrix, I want the >output in a certain order...df2<-as.data.frame(matrix(1:4,nrow=2,ncol=2),row.names=c("R3","R4")) df2<-df2[c(2,1),]>3) How to I "append" to the bottom of a dataframe?df2<-rbind(df2,c(5,6)) Xiaohui Chen Dept. of Statistics UBC, Canada>From: Jonathan Greenberg <jgreenberg at arc.nasa.gov> >To: R-help <r-help at stat.math.ethz.ch> >Subject: [R] Data frames questions >Date: Sat, 23 Sep 2006 14:54:00 -0700 > >Hi there, couple of questions on data frames: > >1) Is there a way to build an empty data frame, containing nothing but the >data frame variable names? >2) Is there a way to reorder the variables in a data frame, e.g. When I go >to write out a data frame using write.table or write.matrix, I want the >output in a certain order... >3) How to I "append" to the bottom of a dataframe? > >Thanks! > >--j > >-- >Jonathan A. Greenberg, PhD >NRC Research Associate >NASA Ames Research Center >MS 242-4 >Moffett Field, CA 94035-1000 >Office: 650-604-5896 >Cell: 415-794-5043 >AIM: jgrn307 >MSN: jgrn307 at hotmail.com > >______________________________________________ >R-help at stat.math.ethz.ch 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.
> 3) How to I "append" to the bottom of a dataframe?APPEND ROWS ITERATIVELY TO A DATA FRAME ################################################# # APPEND ROWS ITERATIVELY TO A DATA FRAME # ################################################# # LOOP FROM 1 TO 10 for(i in 1:10) { # SET THE RANDOM STATE set.seed(i); # CREATE A DUMMY DATA FRAME ITERATIVELY data<-data.frame(iter = i, x = rnorm(1), y = runif(1)); # ASSIGN THE ROW NAME row.names(data)<-i; # APPEND NEW DATA TO THE END OF MAIN DATA FRAME if (i == 1) main<-data else main<-rbind(main, data); };
> -----Original Message----- > From: X.H Chen [mailto:xchen_stat at hotmail.com] > Sent: Sunday, September 24, 2006 12:16 AM > To: jgreenberg at arc.nasa.gov; r-help at stat.math.ethz.ch > Subject: Re: [R] Data frames questions > > > >1) Is there a way to build an empty data frame, containing > nothing but > >the data frame variable names? > > Yes, you can do it one way as followings: > > df1<-as.data.frame(matrix(nrow=2,ncol=2),row.names=c("R1","R2"))This results in a dataframe with two rows and two columns NA in it. In order to build an empty dataframe one you could use: df1 <- data.frame( X1=1, x2=3, l1=TRUE, d3=as.Date("2003-07-14") )[NULL,] str( df1 ) The key is the "NULL" for the rows, it removes the data but keeps the content. Best, Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ---------------------- (snip)