Dear all, I have different data frames for which I would like to modify names of each column such that the new name would include the name of the first column added to the name of other columns; I came up with the following code. Nothing changes when I run the following code. I would be grateful if someone could help me. All the best, -Arman rename_columns <- function(dataset) { for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1], names(dataset)[i], sep="_") } } rename_columns(dataset) %nothing happens! [[alternative HTML version deleted]]
Hi, rename_columns<- function(dat){ ??? for(i in 2:(ncol(dat))){ ??? names(dat)[i]<- paste(names(dat)[1],names(dat)[i],sep="_") ??? ??? } dat } dat1<- read.table(text=" chr??? pos??? ref??? alt chr1??? 5??? A??? G chr1??? 8??? T??? C chr2??? 2??? C??? T ",sep="",header=TRUE,stringsAsFactors=FALSE) ?rename_columns(dat1) #?? chr chr_pos chr_ref chr_alt #1 chr1?????? 5?????? A?????? G #2 chr1?????? 8?????? T?????? C #3 chr2?????? 2?????? C?????? T A.K. ----- Original Message ----- From: Arman Eshaghi <arman.eshaghi at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, June 14, 2013 9:34 AM Subject: [R] rename and concatenate name of columns Dear all, I have different data frames for which I would like to modify names of each column such that the new name would include the name of the first column added to the name of other columns; I came up with the following code. Nothing changes when I run the following code. I would be grateful if someone could help me. All the best, -Arman rename_columns <- function(dataset) { for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1], names(dataset)[i], sep="_") } } rename_columns(dataset) %nothing happens! ??? [[alternative HTML version deleted]] ______________________________________________ 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.
df1 <- data.frame( A = runif( 10 ), B = runif( 10 ) * 5, C = runif( 10 ) * 10, D = runif( 10 ) * 20 ) df2 <- data.frame( X = runif( 10 ), Y = runif( 10 ) * 5, Z = runif( 10 ) * 10 ) rename_columns <- function( dataset ) { for( i in 2:ncol( dataset ) ) colnames( dataset )[i] <- paste( colnames( dataset )[1], colnames( dataset )[i], sep = "_" ) return( dataset ) } df1 <- rename_columns( df1 ) df1 A A_B A_C A_D 1 0.79914742 4.4898911 2.8869670979 11.067921 2 0.34552180 3.5456202 7.4774973304 8.610913 3 0.02950957 3.1754457 2.4493258283 17.649965 4 0.41804920 3.6180818 2.4680344155 5.038590 5 0.69496877 1.7169792 0.8790624910 3.476689 6 0.79293910 0.4452258 9.5359219401 3.668066 7 0.06135734 0.9188141 8.4256720915 9.980292 8 0.86066946 1.2674735 4.1982888198 13.561160 9 0.41895427 0.1434889 0.0007853331 3.187101 10 0.09498189 4.5215823 1.0008131783 10.428556 On Friday 14 June 2013 18:04:22 Arman Eshaghi wrote:> Dear all, > > I have different data frames for which I would like to modify names of each > column such that the new name would include the name of the first column > added to the name of other columns; I came up with the following code. > Nothing changes when I run the following code. I would be grateful if > someone could help me. > > All the best, > -Arman > > rename_columns <- function(dataset) { > for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1], > names(dataset)[i], sep="_") > } > } > > rename_columns(dataset) %nothing happens! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Jun 14, 2013, at 6:34 AM, Arman Eshaghi wrote:> Dear all, > > I have different data frames for which I would like to modify names of each > column such that the new name would include the name of the first column > added to the name of other columns; I came up with the following code. > Nothing changes when I run the following code. I would be grateful if > someone could help me. > > All the best, > -Arman > > rename_columns <- function(dataset) { > for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1], > names(dataset)[i], sep="_") > } > } > > rename_columns(dataset) %nothing happens!A bit of commentary: Something did happen. It's just that you didn't do anything with _what_ happened. The copy of the 'dataset'-object got modified but you never returned it from the function, and and also didn't reassign it to the original 'dataset'. Functions return their last assignment. In the case of the 'for'-function, it somewhat surprisingly returns a NULL. It is a rather odd function in the functional R world, since its main role in life is doing things by side-effects, and so when used inside functions has seemingly paradoxical behavior. Check your results with: rename_columns <- function(dataset) { for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1], names(dataset)[i], sep="_") } dataset} dataset <- rename_columns(dataset)> [[alternative HTML version deleted]]And do learn to post in plain text.> > ______________________________________________ > 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.David Winsemius Alameda, CA, USA