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. Thanks Louize
Is names(d2) <- c("year","av_t1") what you're looking for? steve> -----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 7: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. > Thanks > Louize > > ______________________________________________ > 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
I suspect you are interpreting the column names as a column, whereas in fact you only have a single column data frame, so using names(d2) <- "whatever" will work. Thanks, Alec Alec Stephenson Department of Statistics Macquarie University NSW 2109, Australia>>> louize99 at yahoo.co.uk 08/20/04 10:10am >>>apparently not that gives me the following error: Error in "names<-.default"(`*tmp*`, value = c("year", "av_t1")) : names attribute [2] must be the same length as the vector [1] presumably because the 2nd column (av_tl) already has a column name, and therefore seems longer? ----- Original Message ----- From: "Steve Upton" <supton at referentia.com> To: "'Louize Hill'" <lhill at ipimar.pt>; <r-help at stat.math.ethz.ch> Sent: Friday, August 20, 2004 12:35 AM Subject: RE: [R] column names in data.frame> Is names(d2) <- c("year","av_t1") what you're looking for? > > steve > > > -----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 7: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 hasa> > 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 problemor find> > a > > similar problem in the archives. > > Thanks > > Louize > > > > ______________________________________________ > > 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 > >______________________________________________ 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
Hi Steve and Alec, Thanks for your help - the 2nd column does have a column name ... see below:> d2av_tl 1979 3.520333 1980 3.513684 etc ... or maybe I only have 1 column, as I can change "av_tl" to "year", "whatever"... but then what is the list of dates (labels)? I need to put a name to this column for subsequent analysis. Thanks Louize ----- Original Message ----- From: "Alec Stephenson" <astephen at efs.mq.edu.au> To: <lhill at ipimar.pt>; <supton at referentia.com>; <r-help at stat.math.ethz.ch>; <louize99 at yahoo.co.uk> Sent: Friday, August 20, 2004 1:36 AM Subject: Re: [R] column names in data.frame> I suspect you are interpreting the column names as a column, whereas in > fact you only have a single column data frame, so using > names(d2) <- "whatever" > will work. > > Thanks, > Alec > > > > Alec Stephenson > Department of Statistics > Macquarie University > NSW 2109, Australia > > >>> louize99 at yahoo.co.uk 08/20/04 10:10am >>> > apparently not > that gives me the following error: > > Error in "names<-.default"(`*tmp*`, value = c("year", "av_t1")) : > names attribute [2] must be the same length as the vector [1] > > presumably because the 2nd column (av_tl) already has a column name, > and > therefore seems longer? > > > ----- Original Message ----- > From: "Steve Upton" <supton at referentia.com> > To: "'Louize Hill'" <lhill at ipimar.pt>; <r-help at stat.math.ethz.ch> > Sent: Friday, August 20, 2004 12:35 AM > Subject: RE: [R] column names in data.frame > > > > Is names(d2) <- c("year","av_t1") what you're looking for? > > > > steve > > > > > -----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 7: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. > > > Thanks > > > Louize > > > > > > ______________________________________________ > > > 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 > > > > > > ______________________________________________ > 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 >
Your years are your row names; if you want to use them in an analysis, it may be easiest to create a new column of years using cbind. Alec Stephenson Department of Statistics Macquarie University NSW 2109, Australia>>> louize99 at yahoo.co.uk 08/20/04 10:43am >>>Hi Steve and Alec, Thanks for your help - the 2nd column does have a column name ... see below:> d2av_tl 1979 3.520333 1980 3.513684 etc ... or maybe I only have 1 column, as I can change "av_tl" to "year", "whatever"... but then what is the list of dates (labels)? I need to put a name to this column for subsequent analysis. Thanks Louize ----- Original Message ----- From: "Alec Stephenson" <astephen at efs.mq.edu.au> To: <lhill at ipimar.pt>; <supton at referentia.com>; <r-help at stat.math.ethz.ch>; <louize99 at yahoo.co.uk> Sent: Friday, August 20, 2004 1:36 AM Subject: Re: [R] column names in data.frame> I suspect you are interpreting the column names as a column, whereasin> fact you only have a single column data frame, so using > names(d2) <- "whatever" > will work. > > Thanks, > Alec > > > > Alec Stephenson > Department of Statistics > Macquarie University > NSW 2109, Australia > > >>> louize99 at yahoo.co.uk 08/20/04 10:10am >>> > apparently not > that gives me the following error: > > Error in "names<-.default"(`*tmp*`, value = c("year", "av_t1")) : > names attribute [2] must be the same length as the vector[1]> > presumably because the 2nd column (av_tl) already has a column name, > and > therefore seems longer? > > > ----- Original Message ----- > From: "Steve Upton" <supton at referentia.com> > To: "'Louize Hill'" <lhill at ipimar.pt>; <r-help at stat.math.ethz.ch> > Sent: Friday, August 20, 2004 12:35 AM > Subject: RE: [R] column names in data.frame > > > > Is names(d2) <- c("year","av_t1") what you're looking for? > > > > steve > > > > > -----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 7: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 whichhas> 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 myproblem> or > find > > > a > > > similar problem in the archives. > > > Thanks > > > Louize > > > > > > ______________________________________________ > > > 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 > > > > > > ______________________________________________ > 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 >
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)) or data.frame(av.tl, year=names(av.tl)) Hope this helps, --Matt -----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. Thanks Louize ______________________________________________ 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