Johannes Radinger
2012-Dec-11 10:55 UTC
[R] Renaming column names according to another dataframe
Hi, I've got a dataframe having a code as column name. Addtionally I have another dataframe with a two columns (and lots of rows), the first containing the code and the second some Text (real name). Now I'd like to use the information (pairs of code and name) of the second dataframe to rename all the columnnames in the first dataframe. How is it possible to achieve that? Here a small example of the two dataframes: df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col B","Col C","Col D","Col E")) /j
Anthony Damico
2012-Dec-11 11:47 UTC
[R] Renaming column names according to another dataframe
df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) # my changes to show that order doesn't matter df_names <- data.frame(code=c("C","A","D","E","B"),name=c("Col C","Col A","Col D","Col E","Col B")) names( df ) <- df_names[ match( names( df ) , df_names[ , 'code' ] ) , 'name' ] for more detail see ?match On Tue, Dec 11, 2012 at 5:55 AM, Johannes Radinger < johannesradinger@gmail.com> wrote:> Hi, > > I've got a dataframe having a code as column name. > Addtionally I have another dataframe with a two columns (and lots of > rows), the first > containing the code and the second some Text (real name). > > Now I'd like to use the information (pairs of code and name) of the > second dataframe to rename all the columnnames in the first dataframe. > How is it possible to achieve that? > > Here a small example of the two dataframes: > > df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) > df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col > B","Col C","Col D","Col E")) > > /j > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
HI, Try this: ?names(df)<-df_names$name[df_names$code%in%names(df)] ?head(df,2) #? Col A Col B Col C #1???? 1???? 1???? 1 #2???? 2???? 2???? 2 A.K. ----- Original Message ----- From: Johannes Radinger <johannesradinger at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, December 11, 2012 5:55 AM Subject: [R] Renaming column names according to another dataframe Hi, I've got a dataframe having a code as column name. Addtionally I have another dataframe with a two columns (and lots of rows), the first containing the code and the second some Text (real name). Now I'd like to use the information (pairs of code and name) of the second dataframe to rename all the columnnames in the first dataframe. How is it possible to achieve that? Here a small example of the two dataframes: df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col B","Col C","Col D","Col E")) /j ______________________________________________ R-help at r-project.org 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.
HI, You can also try this: df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) df_names <- data.frame(code=c("A","B","D","E","C"),name=c("Col A","Col B","Col D","Col E","Col C")) names(df)<-df_names$name[match(names(df),df_names$code)] A.K. ----- Original Message ----- From: Johannes Radinger <johannesradinger at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, December 11, 2012 5:55 AM Subject: [R] Renaming column names according to another dataframe Hi, I've got a dataframe having a code as column name. Addtionally I have another dataframe with a two columns (and lots of rows), the first containing the code and the second some Text (real name). Now I'd like to use the information (pairs of code and name) of the second dataframe to rename all the columnnames in the first dataframe. How is it possible to achieve that? Here a small example of the two dataframes: df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col B","Col C","Col D","Col E")) /j ______________________________________________ R-help at r-project.org 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.
Johannes Radinger
2012-Dec-11 12:52 UTC
[R] Renaming column names according to another dataframe
Hi, thank you so much, that works perfectly. I used the first suggestion by Anthony (names( df ) <- df_names[ match( names( df ) , df_names[ , 'code' ] ) , 'name' ]). Thank you for the hint about ?match... that was the function I was looking for, makes comparing vectors very easy. best, /Johannes On Tue, Dec 11, 2012 at 1:06 PM, arun <smartpink111 at yahoo.com> wrote:> HI, > > You can also try this: > df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) > df_names <- data.frame(code=c("A","B","D","E","C"),name=c("Col A","Col B","Col D","Col E","Col C")) > > names(df)<-df_names$name[match(names(df),df_names$code)] > > > A.K. > > ----- Original Message ----- > From: Johannes Radinger <johannesradinger at gmail.com> > To: r-help at r-project.org > Cc: > Sent: Tuesday, December 11, 2012 5:55 AM > Subject: [R] Renaming column names according to another dataframe > > Hi, > > I've got a dataframe having a code as column name. > Addtionally I have another dataframe with a two columns (and lots of > rows), the first > containing the code and the second some Text (real name). > > Now I'd like to use the information (pairs of code and name) of the > second dataframe to rename all the columnnames in the first dataframe. > How is it possible to achieve that? > > Here a small example of the two dataframes: > > df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) > df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col > B","Col C","Col D","Col E")) > > /j > > ______________________________________________ > R-help at r-project.org 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. >
Hi, I've got a dataframe having a code as column name. Addtionally I have another dataframe with a two columns (and lots of rows), the first containing the code and the second some Text (real name). Now I'd like to use the information (pairs of code and name) of the second dataframe to rename all the columnnames in the first dataframe. How is it possible to achieve that? Here a small example of the two dataframes: df <- data.frame(A=(1:10),B=(1:10),C=(1:10)) df_names <- data.frame(code=c("A","B","C","D","E"),name=c("Col A","Col B","Col C","Col D","Col E")) click here <http://totalltelugumovies.blogspot.in> -- View this message in context: http://r.789695.n4.nabble.com/Renaming-column-names-according-to-another-dataframe-tp4653824.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]