[R] column names in data.frame

Austin, Matt maustin at amgen.com
Fri Aug 20 03:27:34 CEST 2004

Note that split() creates a list with each component named by the year.
When you use the sapply function the names are retained in the attribute
"names", you can print this out to see or use str() or look at the output
from the attribute() function.

Your division also maintains the names and then when you coerce av.tl into a
dataframe they are translated to row names.  Check out the output from
row.names(d2).  If you want these as part of your dataframe you could so
something like

data.frame(d2, year=row.names(d2))
data.frame(av.tl, year=names(av.tl))

Hope this helps,


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Louize Hill
Sent: Thursday, August 19, 2004 16:26 PM
To: r-help at stat.math.ethz.ch
Subject: [R] column names in data.frame

Dear R-help,
Please can someone explain how to put a column name on an output data.frame.

##Starting with a data.frame with 3 columns (d$Year, d$NoIndiv, d$wtd_tl)

yr_ind <- split (d$NoIndiv, d$Year)
yr_tl <- split (d$wtd_tl, d$Year)

ann_ind <- sapply (yr_ind, sum)
ann_tl <- sapply (yr_tl, sum)

av_tl <- ann_tl/ann_ind

d2<- data.frame (av_tl)

##This gives me a data.frame with columns, the second of which has a column
name (av_tl)
I have tried
> d2<- data.frame (year = x, av_tl)
> d2<- data.frame (x="year", av_tl)
> d2<- data.frame (x="year", av_tl, check.names = TRUE)
> d2<- data.frame (year, av_tl)

as well as several combinations with cbind, as.matrix, etc...
I cannot relate the examples given in ?data.frame with my problem or find a
similar problem in the archives.

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list