How to change the name of a data frame in R?



To change the name of a data frame, we can set the original name to the new name. Now both of the names can be used. Most of the times the purpose behind changing the name of the data frame is that, the original name does not seem to be a valid name based on the characteristics of the data. For example, if we have normally distributed columns in the data frame then we can name it as normal_distribution. This will help everyone to understand the data belongs to normal distribution.

Example1

 Live Demo

set.seed(24) x<−rnorm(20,1,0.25) df1<−data.frame(x) df1

Output

 x 1 0.8635298 2 1.1341463 3 1.1049058 4 0.8540932 5 1.2118650 6 1.0665055 7 1.1111463 8 0.8833762 9 0.7879075 10 1.0005780 11 0.6707730 12 1.1495673 13 0.8094464 14 0.6427274 15 1.0830611 16 0.8827348 17 0.9162533 18 1.3840630 19 1.1524986 20 1.1290839

Changing the name of df1 to Normal_Distribution −

Example

Normal_Distribution<−df1 Normal_Distribution

Output

 x 1 0.8635298 2 1.1341463 3 1.1049058 4 0.8540932 5 1.2118650 6 1.0665055 7 1.1111463 8 0.8833762 9 0.7879075 10 1.0005780 11 0.6707730 12 1.1495673 13 0.8094464 14 0.6427274 15 1.0830611 16 0.8827348 17 0.9162533 18 1.3840630 19 1.1524986 20 1.1290839

Example2

 Live Demo

y<−sample(0:5,20,replace=TRUE) df2<−data.frame(y) df2

Output

 y 1 4 2 2 3 2 4 3 5 3 6 1 7 1 8 2 9 0 10 4 11 4 12 3 13 5 14 1 15 0 16 0 17 4 18 2 19 2 20 5

Changing the name of df2 to Random_Sample −

Example

Random_Sample<−df2 Random_Sample

Output

 y 1 4 2 2 3 2 4 3 5 3 6 1 7 1 8 2 9 0 10 4 11 4 12 3 13 5 14 1 15 0 16 0 17 4 18 2 19 2 20 5
Updated on: 2021-02-05T10:57:44+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements