How to change Column name of a given DataFrame in R

Column names of a R Data casing can be gotten to utilizing the capacity colnames(). We can likewise get to the singular section names utilizing a record to the result of colnames() very much like a cluster with documentation colnames(df)[index].

To rename segments of a R Data Frame, dole out colnames(dataframe) with the necessary vector of segment names. To change a solitary section name, we might utilize list documentation.

An information outline is a plain design with fixed aspects, of each lines just as segments. It is a two-layered cluster like item with mathematical, character based or factor-type information. Every component having a place with the information outline is listed by a special blend of the line and section number separately. Segment names are tended to by one of a kind names.

1: utilizing colnames() technique

colnames() strategy in R is utilized to rename and supplant the section names of the information outline in R.

The segments of the information casing can be renamed by indicating the new segment names as a vector. The new name replaces the comparing old name of the section in the information outline. The length of new section vector ought to be comparable to the quantity of segments initially. Changes are made to the first information outline.

Syntax:

colnames(df) <- c(new_col1_name,new_col2_name,new_col3_name)

Example:

# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', 'J', 'E', NA,'M'),
col2 = c(12.5, 9, 16.5, NA, 9, 20, 14.5),
col3 = c(NA, 3, 2, NA, 1, NA, 0))
 
# printing original data frame
print("Original data frame : ")
print(df)
 
print("Renaming columns names ")
 
# assigning new names to the columns of the data frame
colnames(df) <- c('C1','C2','C3')
 
# printing new data frame
print("New data frame : ")
print(df)

Output:

[1] “Original data frame : “

col1 col2 col3

1    A 12.5   NA

2    B  9.0    3

3    C 16.5    2

4    J   NA   NA

5    E  9.0    1

6 <NA> 20.0   NA

7    M 14.5    0

[1] “Renaming columns names “

[1] “New data frame : “

C1   C2 C3

1    A 12.5 NA

2    B  9.0  3

3    C 16.5  2

4    J   NA NA

5    E  9.0  1

6 <NA> 20.0 NA

7    M 14.5  0

1(A)

Syntax:

colnames(df)[col_indx] <- “new_col_name_at_col_indx”

Approach

  • Create dataframe
  • Select the column to be renamed by index
  • Provide a suitable name
  • Change using colnames() function

Example:

# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', 'J', 'E', NA,'M'),
col2 = c(12.5, 9, 16.5, NA, 9, 20, 14.5),
col3 = c(NA, 3, 2, NA, 1, NA, 0))
 
# printing original data frame
print("Original data frame : ")
print(df)
 
print("Renaming columns names ")
 
# assigning the second column name to a new name
colnames(df)[2] <- "new_col2"
 
# printing new data frame
print("New data frame : ")
print(df)

Output:

[1] “Original data frame : “

col1 col2 col3

1    A 12.5   NA

2    B  9.0    3

3    C 16.5    2

4    J   NA   NA

5    E  9.0    1

6 <NA> 20.0   NA

7    M 14.5    0

[1] “Renaming columns names “

[1] “New data frame : “

col1 new_col2 col3

1    A     12.5   NA

2    B      9.0    3

3    C     16.5    2

4    J       NA   NA

5    E      9.0    1

6 <NA>     20.0   NA

7    M     14.5    0

1(B).

Syntax:

colnames(dataframe)[which(names(dataframe) == “oldColName”)] <- “newColName”

Approach

  • Create data frame
  • Select name of the columns to be changed
  • Provide a suitable name
  • Use the function

Example:

# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', NA,'M'),
col2 = c(12.5, 9, 16.5,  20, 14.5),
col3 = c(NA, 3, 2, NA, 0))
 
# printing original data frame
print("Original data frame : ")
print(df)
 
print("Renaming columns names ")
# assigning the second column name to a new name
 
colnames(df)[2] <- "new_col2"
 
# printing new data frame
print("After changing the data frame col2 name : ")
print(df)
 
# replacing first column name
colnames(df)[which(names(df) == "col1")] <- "new_col1"
 
# printing new data frame
print("After changing the data frame col1 name : ")
print(df)

Output

[1] “Original data frame : “

col1 col2 col3

1    A 12.5   NA

2    B  9.0    3

3    C 16.5    2

4 <NA> 20.0   NA

5    M 14.5    0

[1] “Renaming columns names “

[1] “After changing the data frame col2 name : “

col1 new_col2 col3

1    A     12.5   NA

2    B      9.0    3

3    C     16.5    2

4 <NA>     20.0   NA

5    M     14.5    0

[1] “After changing the data frame col1 name : “

new_col1 new_col2 col3

1        A     12.5   NA

2        B      9.0    3

3        C     16.5    2

4     <NA>     20.0   NA

5        M     14.5    0

2: using setNames() method

Syntax:

setnames(df, c(names of new columns))

Approach

  • Create data frame
  • Rename column using function
  • Display modified data frame

Example:

# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', NA,'M'),
col2 = c(12.5, 9, 16.5,  20, 14.5),
col3 = c(NA, 3, 2, NA, 0))
 
# printing original data frame
print("Original data frame : ")
print(df)
 
# print("Renaming columns names ")
# renaming all the column names of data frame
df <- setNames(df, c("changed_Col1","changed_Col2","changed_Col3"))
 
print("Renamed data frame : ")
print(df)

Output

[1] “Original data frame : “

col1 col2 col3

1    A 12.5   NA

2    B  9.0    3

3    C 16.5    2

4 <NA> 20.0   NA

5    M 14.5    0

[1] “Renamed data frame : “

changed_Col1 changed_Col2 changed_Col3

1            A         12.5           NA

2            B          9.0            3

3            C         16.5            2

4         <NA>         20.0           NA

5            M         14.5            0

Also Read: Reading an Excel File using Python

Leave a Reply

Your email address will not be published.