Re: [R] Better way to change the name of a column in a dataframe?
And there is the rename.vars() function in the gdata package. -Don At 11:39 AM -0600 12/14/06, Ben Fairbank wrote: >Hello R users -- > > > >If I have a dataframe such as the following, named "frame" with the >columns intended to be named col1 through col6, > > > >> frame > > col1 col2 cmlo3 col4 col5 col6 > >[1,]3 10 2657 > >[2,]68 4 1071 > >[3,]75 1318 > >[4,] 106 5492 > > > >and I want to correct or otherwise change the name of one of the >columns, I can do so with > > > >> dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3" > > > >which renames the offending column: > > > >> frame > > col1 col2 col3 col4 col5 col6 > >[1,]3 102657 > >[2,]684 1071 > >[3,]751318 > >[4,] 1065492 > > > >This seems cumbersome and not very intuitive. How can one accomplish >this more simply? > > > >With thanks for any suggestions, > > > >Ben Fairbank > > > [[alternative HTML version deleted]] > >__ >R-help@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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA __ R-help@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.
Re: [R] Better way to change the name of a column in a dataframe?
Ben Fairbank wrote: > Hello R users -- > > > > If I have a dataframe such as the following, named "frame" with the > columns intended to be named col1 through col6, > > > >> frame > > col1 col2 cmlo3 col4 col5 col6 > > [1,]3 10 2657 > > [2,]68 4 1071 > > [3,]75 1318 > > [4,] 106 5492 > > > > and I want to correct or otherwise change the name of one of the > columns, I can do so with > > > >> dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3" > > > > which renames the offending column: > > > >> frame > > col1 col2 col3 col4 col5 col6 > > [1,]3 102657 > > [2,]684 1071 > > [3,]751318 > > [4,] 1065492 > > > > This seems cumbersome and not very intuitive. How can one accomplish > this more simply? > > well I would simply use names(frame)[3] = 'col3' (supposing you know the column number of your offending column anyway). __ R-help@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.
Re: [R] Better way to change the name of a column in a dataframe?
Hi, names(frame)[names(frame) == "cmlo3"] <- "col3" should also work Cheers Andrew On Thu, Dec 14, 2006 at 05:05:20PM -0500, Mike Prager wrote: > "Ben Fairbank" <[EMAIL PROTECTED]> wrote: > > > [...] I want to correct or otherwise change the name of one of the > > columns, I can do so with > > > > > dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3" > > > > This seems cumbersome and not very intuitive. How can one accomplish > > this more simply? > > This is slightly simpler than what you had: > > names(frame)[which(names(frame) == "cmlo3")] <- "col3" > > There are probably better ways still. > > -- > Mike Prager, NOAA, Beaufort, NC > * Opinions expressed are personal and not represented otherwise. > * Any use of tradenames does not constitute a NOAA endorsement. > > __ > R-help@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. -- Andrew Robinson Department of Mathematics and StatisticsTel: +61-3-8344-9763 University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr http://blogs.mbs.edu/fishing-in-the-bay/ __ R-help@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.
Re: [R] Better way to change the name of a column in a dataframe?
"Ben Fairbank" <[EMAIL PROTECTED]> wrote: > [...] I want to correct or otherwise change the name of one of the > columns, I can do so with > > > dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3" > > This seems cumbersome and not very intuitive. How can one accomplish > this more simply? This is slightly simpler than what you had: names(frame)[which(names(frame) == "cmlo3")] <- "col3" There are probably better ways still. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement. __ R-help@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] Better way to change the name of a column in a dataframe?
Ben, Unless I misunderstand your question, why not just use names(frame)[3]<-"col3" __ R-help@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] Better way to change the name of a column in a dataframe?
Hello R users -- If I have a dataframe such as the following, named "frame" with the columns intended to be named col1 through col6, > frame col1 col2 cmlo3 col4 col5 col6 [1,]3 10 2657 [2,]68 4 1071 [3,]75 1318 [4,] 106 5492 and I want to correct or otherwise change the name of one of the columns, I can do so with > dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3" which renames the offending column: > frame col1 col2 col3 col4 col5 col6 [1,]3 102657 [2,]684 1071 [3,]751318 [4,] 1065492 This seems cumbersome and not very intuitive. How can one accomplish this more simply? With thanks for any suggestions, Ben Fairbank [[alternative HTML version deleted]] __ R-help@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.