Ken Termiso
2005-Feb-08 18:47 UTC
[R] Renaming columns in data.frame, inserting/removing columns from data.frame
Hello, I'm hoping that there is an easier way to rename columns in a data frame other than by using the names() assignment, which requires you to type in all the column names at once for a data.frame, in the case that I simply want to rename a single column in a data frame. Also, is there an easy way to move columns in a data frame around relative to the other columns? Thanks in advance, Ken
Sean Davis
2005-Feb-08 19:34 UTC
[R] Renaming columns in data.frame, inserting/removing columns from data.frame
On Feb 8, 2005, at 1:47 PM, Ken Termiso wrote:> Hello, > > I'm hoping that there is an easier way to rename columns in a data > frame other than by using the names() assignment, which requires you > to type in all the column names at once for a data.frame, in the case > that I simply want to rename a single column in a data frame. >If you want to change (or name) only the name in column 12, for example, names(mydataframe)[12] <- 'new column 12 name' will do the trick.> Also, is there an easy way to move columns in a data frame around > relative to the other columns?You can do this by assigning your rearranged dataframe back to itself (carefully): mydataframe <- mydataframe[c(5:10,1:4),] Will take the columns 5 through 10 and put them before columns 1 through 4. I note "carefully" above because if mydataframe contains, for example, 12 columns, the command here would drop columns 11 and 12. Hope this helps. Sean
Witold Eryk Wolski
2005-Feb-08 19:38 UTC
[R] Renaming columns in data.frame, inserting/removing columns from data.frame
Ken Termiso wrote:> Hello, > > I'm hoping that there is an easier way to rename columns in a data > frame other than by using the names() assignment, which requires you > to type in all the column names at once for a data.frame, in the case > that I simply want to rename a single column in a data frame. >names(mydataframe)[column.index]<-"new.name"> Also, is there an easy way to move columns in a data frame around > relative to the other columns? >?subset or mydataframe[c("column3","column2","column1")]> Thanks in advance, > Ken > > ______________________________________________ > 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 >-- Dipl. bio-chem. Witold Eryk Wolski MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin tel: 0049-30-83875219 __("< _ http://www.molgen.mpg.de/~wolski \__/ 'v' http://r4proteomics.sourceforge.net || / \ mail: witek96 at users.sourceforge.net ^^ m m wolski at molgen.mpg.de
Mike Prager
2005-Feb-08 20:15 UTC
[R] Renaming columns in data.frame, inserting/removing columns from data.frame
At 2/8/2005 01:47 PM, Ken Termiso wrote:>I'm hoping that there is an easier way to rename columns in a data frame >other than by using the names() assignment, which requires you to type in >all the column names at once for a data.frame, in the case that I simply >want to rename a single column in a data frame.This is ugly but it works, and it avoids the use column indices (which I find error-prone): > aa = data.frame(a=1:6, b=5:10) # a simple example > aa a b 1 1 5 2 2 6 3 3 7 4 4 8 5 5 9 6 6 10 > names(aa)[names(aa)=="a"] = "c" # rename "a" to "c" > aa c b 1 1 5 2 2 6 3 3 7 4 4 8 5 5 9 6 6 10>Also, is there an easy way to move columns in a data frame around relative >to the other columns?Ditto: > aa = data.frame(aa["b"],aa["c"]) > aa b c 1 5 1 2 6 2 3 7 3 4 8 4 5 9 5 6 10 6 MHP -- Michael Prager NOAA Center for Coastal Fisheries and Habitat Research Beaufort, North Carolina 28516 USA http://shrimp.ccfhrb.noaa.gov/~mprager/ NOTE: Opinions expressed are personal, not official. No government endorsement of any product is made or implied.
Mike Prager
2005-Feb-08 20:43 UTC
[R] Renaming columns in data.frame, inserting/removing columns from data.frame
At 2/8/2005 01:47 PM, Ken Termiso wrote:>I'm hoping that there is an easier way to rename columns in a data frame >other than by using the names() assignment, which requires you to type in >all the column names at once for a data.frame, in the case that I simply >want to rename a single column in a data frame.This is ugly but it works, and it avoids the use column indices (which I find error-prone): > aa = data.frame(a=1:6, b=5:10) # a simple example > aa a b 1 1 5 2 2 6 3 3 7 4 4 8 5 5 9 6 6 10 > names(aa)[names(aa)=="a"] = "c" # rename "a" to "c" > aa c b 1 1 5 2 2 6 3 3 7 4 4 8 5 5 9 6 6 10>Also, is there an easy way to move columns in a data frame around relative >to the other columns?Ditto: > aa = data.frame(aa["b"],aa["c"]) > aa b c 1 5 1 2 6 2 3 7 3 4 8 4 5 9 5 6 10 6 MHP -- Michael Prager NOAA Center for Coastal Fisheries and Habitat Research Beaufort, North Carolina 28516 USA http://shrimp.ccfhrb.noaa.gov/~mprager/ NOTE: Opinions expressed are personal, not official. No government endorsement of any product is made or implied.