BLOG

Change column name of a given DataFrame in R

84

A data frame is a tabular structure with fixed dimensions, of each rows as well as columns. It is a two-dimensional array like object with numerical, character based or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respectively. Column names are addressed by unique names.

Method 1: using colnames() method

colnames() method in R is used to rename and replace the column names of the data frame in R.

Bạn Đang Xem: Change column name of a given DataFrame in R

Read more: R how to change column names

The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame. The length of new column vector should be equivalent to the number of columns originally. Changes are made to the original data frame.

Syntax:

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

Example:

R

# declaring the columns of data framedf = cinemaboxhd.orge(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 frameprint(“Original data frame : “)print(df) print(“Renaming columns names “) # assigning new names to the columns of the data framecolnames(df) <- c(‘C1′,’C2′,’C3’) # printing new data frameprint(“New data frame : “)print(df)

Output:

Xem Thêm : Toei Delays Dragon Ball Super: Super Hero

[1] “Original data frame : “

Watch more: How to See What Font a Website Is Using

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) .Specific columns of the data frame can also be renamed using the position index of the respective column.

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:

R

# declaring the columns of data framedf = cinemaboxhd.orge(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 frameprint(“Original data frame : “)print(df) print(“Renaming columns names “) # assigning the second column name to a new namecolnames(df)[2] <- “new_col2” # printing new data frameprint(“New data frame : “)print(df)

Output:

Xem Thêm : Toei Delays Dragon Ball Super: Super Hero

[1] “Original data frame : “

Watch more: How to See What Font a Website Is Using

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 : “

Watch more: 10 Best Dragon Back Tattoo Ideas Youll Have To See To Believe!

Xem Thêm : Hawkeye introduces Maya Lopez, the star of Echo and a key Marvel player

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). Column names can also be replaced by using the which(names(df)) function, which searches for the column with the specified old name and then replaces it with the new specified name instance.

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:

R

# declaring the columns of data framedf = cinemaboxhd.orge(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 frameprint(“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 frameprint(“After changing the data frame col2 name : “)print(df) # replacing first column namecolnames(df)[which(names(df) == “col1”)] <- “new_col1” # printing new data frameprint(“After changing the data frame col1 name : “)print(df)

Output

Xem Thêm : Toei Delays Dragon Ball Super: Super Hero

[1] “Original data frame : “

Watch more: How to See What Font a Website Is Using

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

Watch more: BJP sweeps 8 seats to strengthen its position in Rajya Sabha MVA govt suffers massive setback after BJP wins 3 seats in Maharashtra

[1] “Renaming columns names “

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

Watch more: 10 Best Dragon Back Tattoo Ideas Youll Have To See To Believe!

Xem Thêm : Hawkeye introduces Maya Lopez, the star of Echo and a key Marvel player

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

Watch more: BJP sweeps 8 seats to strengthen its position in Rajya Sabha MVA govt suffers massive setback after BJP wins 3 seats in Maharashtra

[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

Watch more: BJP sweeps 8 seats to strengthen its position in Rajya Sabha MVA govt suffers massive setback after BJP wins 3 seats in Maharashtra

Method 2: using setNames() method

setNames() method in R can also be used to assign new names to the columns contained within a list, vector or tuple. The changes have to be saved back then to the original data frame, because they are not retained.

Syntax:

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

Approach

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

Example:

R

# declaring the columns of data framedf = cinemaboxhd.orge(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 frameprint(“Original data frame : “)print(df) # print(“Renaming columns names “)# renaming all the column names of data framedf <- setNames(df, c(“changed_Col1″,”changed_Col2″,”changed_Col3”)) print(“Renamed data frame : “)print(df)

Output

Xem Thêm : Toei Delays Dragon Ball Super: Super Hero

[1] “Original data frame : “

Watch more: How to See What Font a Website Is Using

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

Watch more: BJP sweeps 8 seats to strengthen its position in Rajya Sabha MVA govt suffers massive setback after BJP wins 3 seats in Maharashtra

[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

Watch more: BJP sweeps 8 seats to strengthen its position in Rajya Sabha MVA govt suffers massive setback after BJP wins 3 seats in Maharashtra

My Personal Notes arrow_drop_upSave

Nguồn: https://cinemaboxhd.org
Danh mục: BLOG

0 ( 0 bình chọn )

IELTS Online Exam & Sample Questions

https://cinemaboxhd.org
IELTS Online Plus, considering your test centre offers this, you will take the Listening, Reading and Writing

Ý kiến bạn đọc (0)

Leave a Reply

Your email address will not be published.

Related Posts

News Post

452. Ledgers Canada

21/07/2022 10:43 77

451. CMB Services

21/07/2022 10:43 75

449. KATM Bookkeeping

21/07/2022 10:41 74

443. Liu & Associates LLP

21/07/2022 10:37 66

442. Liu & Associates

21/07/2022 10:36 57

Xem thêm