How To Add Name To Data Frame Columns In R? - Tutorialspoint
Maybe your like
A data frame can be created by using data.frame function but in this case R generates the column names using the values we pass for the data. For example, if we pass a probability distribution as shown in the below examples then its name will be there. If we want to change those names then setNames function can be used along with the data frame name.
Example
Live Demo
df1<-data.frame(rnorm(20),rnorm(20)) df1Output
rnorm.20. rnorm.20..1 1 -0.24431409 1.48414429 2 -0.04429157 -1.07317747 3 1.03638861 1.23016674 4 -1.51950536 1.01723593 5 -1.29030669 1.59741022 6 -0.02540577 0.16847649 7 0.89598486 1.37993236 8 -1.61080153 -0.89799825 9 -0.54181883 -0.53642593 10 -0.21685719 -0.34991237 11 -0.48691372 0.03656945 12 0.61789184 -0.68130916 13 1.11895194 -1.03310011 14 0.46876609 -0.47855369 15 0.94841120 -0.25945237 16 -0.33669202 0.38229316 17 -0.84262751 -0.62787283 18 1.54774271 -1.97730922 19 -0.23887277 0.02692167 20 -0.33640096 0.35218858Setting names of columns in df1 −
Example
setNames(df1,c("x1","x2"))Output
x1 x2 1 -0.24431409 1.48414429 2 -0.04429157 -1.07317747 3 1.03638861 1.23016674 4 -1.51950536 1.01723593 5 -1.29030669 1.59741022 6 -0.02540577 0.16847649 7 0.89598486 1.37993236 8 -1.61080153 -0.89799825 9 -0.54181883 -0.53642593 10 -0.21685719 -0.34991237 11 -0.48691372 0.03656945 12 0.61789184 -0.68130916 13 1.11895194 -1.03310011 14 0.46876609 -0.47855369 15 0.94841120 -0.25945237 16 -0.33669202 0.38229316 17 -0.84262751 -0.62787283 18 1.54774271 -1.97730922 19 -0.23887277 0.02692167 20 -0.33640096 0.35218858Example
Live Demo
df2<-data.frame(rpois(20,8),rpois(20,5)) df2Output
rpois.20..8. rpois.20..5. 1 7 4 2 12 4 3 6 4 4 7 9 5 11 6 6 8 4 7 8 3 8 7 4 9 4 3 10 10 3 11 7 3 12 12 6 13 8 5 14 11 3 15 6 4 16 8 8 17 6 8 18 10 7 19 11 1 20 5 6Setting names of columns in df2 −
Example
setNames(df2,c("y1","y2"))Output
y1 y2 1 7 4 2 12 4 3 6 4 4 7 9 5 11 6 6 8 4 7 8 3 8 7 4 9 4 3 10 10 3 11 7 3 12 12 6 13 8 5 14 11 3 15 6 4 16 8 8 17 6 8 18 10 7 19 11 1 20 5 6Tag » Add Column Names To Data Frame R
-
8.4 Dataframe Column Names - YaRrr! The Pirate's Guide To R
-
Changing Column Names Of A Data Frame - Stack Overflow
-
Change Column Name Of A Given DataFrame In R - GeeksforGeeks
-
How To Add Header To Dataframe In R ? - GeeksforGeeks
-
Add Header To Data Frame In R (Example) - Statistics Globe
-
How To Add A Column To A Data Frame In R (With Examples)
-
How To Set Column Names In R Code Example - Code Grepper
-
Row And Column Names - R
-
R Data Frame: How To Create, Append, Select & Subset - Guru99
-
Rename Data Frame Columns In R - Datanovia
-
How To Add Header To Pandas Dataframe? - Stack Vidhya
-
How To Add A Column To A Dataframe In R With Tibble & Dplyr
-
R – Rename All Dataframe Column Names - Spark By {Examples}
-
R: Column Names - Apache Spark