[R] rename and concatenate name of columns
Rainer Schuermann
rainer.schuermann at gmx.net
Fri Jun 14 16:10:02 CEST 2013
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.
More information about the R-help
mailing list