[R] Data frames questions
Douglas Bates
bates at stat.wisc.edu
Sun Sep 24 16:27:07 CEST 2006
On 9/23/06, X.H Chen <xchen_stat at hotmail.com> wrote:
> >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),]
I understood the question to be asking how to rearrange the variables
(i.e. columns of the data frame) instead of the rows (observations).
The same technique can be used but with the reordering vector on the
right hand side of the comma. If you have named columns it may be
more accurate to use the names rather than the positions to get the
order you want.
> str(swiss)
`data.frame': 47 obs. of 6 variables:
$ Fertility : num 80.2 83.1 92.5 85.8 76.9 76.1 83.8 92.4 82.4 82.9 ...
$ Agriculture : num 17 45.1 39.7 36.5 43.5 35.3 70.2 67.8 53.3 45.2 ...
$ Examination : int 15 6 5 12 17 9 16 14 12 16 ...
$ Education : int 12 9 5 7 15 7 7 8 7 13 ...
$ Catholic : num 9.96 84.84 93.40 33.77 5.16 ...
$ Infant.Mortality: num 22.2 22.2 20.2 20.3 20.6 26.6 23.6 24.9 21 24.4 ...
> head(swiss)
Fertility Agriculture Examination Education Catholic
Infant.Mortality
Courtelary 80.2 17.0 15 12 9.96
22.2
Delemont 83.1 45.1 6 9 84.84
22.2
Franches-Mnt 92.5 39.7 5 5 93.40
20.2
Moutier 85.8 36.5 12 7 33.77
20.3
Neuveville 76.9 43.5 17 15 5.16
20.6
Porrentruy 76.1 35.3 9 7 90.57
26.6
> head(swiss[, c("Agriculture", "Education", "Examination", "Catholic")])
Agriculture Education Examination Catholic
Courtelary 17.0 12 15 9.96
Delemont 45.1 9 6 84.84
Franches-Mnt 39.7 5 5 93.40
Moutier 36.5 7 12 33.77
Neuveville 43.5 15 17 5.16
Porrentruy 35.3 7 9 90.57
> >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.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list