R Dataframe – Change Column Name

R Dataframe ? Change Column Name

In this tutorial, we will learn how to change column name of R Dataframe.

Column names of an R Dataframe can be acessed using the function colnames() . You can also access the individual column names using an index to the output of colnames() just like an array.

To change all the column names of an R Dataframe, use colnames() as shown in the following syntax

c olnames(mydat afram e) = vec t or_wit h_new_nam es colnames(mydataframe) = vector_with_new_names

To change a single column name of an R Dataframe, use colnames() with index as shown in the following syntax

c olnames(mydat afram e)[index] = new_name

colnames(mydataframe)[index] = new_name

Example to change a single Column Name of Dataframe

In this example, we will create an R dataframe DF1 and change the second column's name to "newC".

> DF1 = data.frame(C1= c(1, 5, 14, 23, 54), C2= c(9, 15, 85, 3, 42), C3= c(9, 7, 42, 87, 16)) > DF1

C1 C2 C3 1 1 9 9 2 5 15 7 3 14 85 42 4 23 3 87 5 54 42 16 > colnames(DF1)[2] = "newC" > DF1

C1 newC C3 11 99 2 5 15 7 3 14 85 42 4 23 3 87 5 54 42 16 >

> DF1 = data.frame(C1= c(1, 5, 14, 23, 54), C2= c(9, 15, 85, 3, 42), C3= c(9, 7, 42, 87, 16)) > DF1

C1 C2 C3 1 1 9 9 2 5 15 7 3 14 85 42 4 23 3 87 5 54 42 16 > colnames(DF1)[2] = "newC" > DF1

C1 newC C3 1 1 9 9 2 5 15 7 3 14 85 42 4 23 3 87 5 54 42 16 >

The column name C2 has changed to newC .

Example to change all Column Names of Dataframe

In this example, we will create an R dataframe DF1 and change all the column's names.

> DF1 = data.frame(C1= c(1, 5, 14, 23, 54), C2= c(9, 15, 85, 3, 42), C3= c>(9D,F71, =42d,a8ta7.,fr1a6m))e(C1= c(1, 5, 14, 23, 54), C2= c(9, 15, 85, 3, 42), C3= c(9, 7, 42, 87, 16)) > DF1 >CD1FC12 C3 1 C11 C92 9C3 2 5 15 7 31 1149859 42 452 255341453278176 >3 c1o4ln8a5m4e2s(DF1) = c("newC1", "newC2", "4ne2w3C33"8)7 >5 D5F4142 16

newC1 newC2 newC3 1> co1lname9s(DF91) = c("newC1", "newC2", "newC3") 2> DF51 15 7 3 14 85 42 4 ne2w3C1 n3ewC827newC3 5 54 42 16 >1 1 9 9 2 5 15 7 3 14 85 42 4 23 3 87 5 54 42 16 >

All the column names have been changed.

Home - Get Started

R Tutorial R Script File R Working Directory R Data Types R Variables R Operators R Vectors R Matrix

Decision Making

R Decision Making R if R if..else R if..else if...else R switch

Loops

R Loops R repeat loop R while loop R for loop R break

Strings

R Strings Find length of String in R Extract Substring from a String in R Concatenate two or more Strings in R

Functions

R Functions

DataFrame

R Data Frame Sort R Data Frame by Column For each row in an R Data Frame

For each row in an R Data Frame Import Excel Data into R Dataframe Convert R Dataframe to Matrix R Dataframe - Delete Rows R Dataframe - Drop Columns R Dataframe - Add Column R Dataframe - Change Column Name R Dataframe - Remove Duplicate Rows R Dataframe - Replace NA with 0 Convert Matrix to R Dataframe

Handling Data from Files

R CSV Files - Read, Filter, Write R Read Excel XLS XLSX files

Charts & Graphs

R Pie Charts R Line Graphs

Statistical Analysis

R Mean of a Vector R Median of a Vector

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download