- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert an old data frame to new data frame in R?
To convert an old data frame to a new data frame, we can simply set the new name. For example, if we have a data frame called df and want to convert it to a new one let’s say df_new then it can be done as df_new<-df. But if we want to change the column names as well then data.frame needs to be used and the new column names will be assigned as shown in the below examples.
Example1
Consider the below data frame −
> x1<-rpois(20,5) > x2<-rpois(20,5) > df1<-data.frame(x1,x2) > df1
Output
x1 x2 1 8 6 2 4 9 3 3 2 4 3 5 5 7 4 6 4 8 7 8 6 8 12 12 9 8 6 10 8 6 11 3 4 12 8 3 13 8 8 14 1 8 15 7 7 16 7 2 17 6 6 18 8 4 19 1 7 20 3 7
Converting df1 to a new data frame −
> new_df1<-data.frame(v1=df1$x1,v2=df1$x2) > new_df1
Output
v1 v2 1 8 6 2 4 9 3 3 2 4 3 5 5 7 4 6 4 8 7 8 6 8 12 12 9 8 6 10 8 6 11 3 4 12 8 3 13 8 8 14 1 8 15 7 7 16 7 2 17 6 6 18 8 4 19 1 7 20 3 7
Example2
> y1<-rnorm(20) > y2<-rnorm(20) > df2<-data.frame(y1,y2) > df2
Output
y1 y2 1 0.16271874 -0.13815830 2 -0.71427991 0.59659353 3 0.91988256 -0.93139535 4 0.21826390 -1.05463645 5 -0.46250555 0.23841747 6 -2.33005442 0.01311722 7 0.07587324 -0.60952309 8 1.69626502 -0.54502419 9 -0.69264529 -1.61676626 10 0.52631934 -0.26636235 11 0.54617330 -1.52785395 12 0.06885748 0.79790822 13 1.21325507 1.36154991 14 -1.30941046 -2.13603119 15 0.87466151 -1.07515475 16 0.48641809 0.58819125 17 -1.06275086 -0.79261408 18 -0.59022569 0.08437131 19 -0.26362438 -1.31612747 20 -1.06148933 -0.36654012
Converting df2 to a new data frame −
> new_df2<-data.frame(Var1=df2$y1,Var2=df2$y2) > new_df2
Output
Var1 Var2 1 0.16271874 -0.13815830 2 -0.71427991 0.59659353 3 0.91988256 -0.93139535 4 0.21826390 -1.05463645 5 -0.46250555 0.23841747 6 -2.33005442 0.01311722 7 0.07587324 -0.60952309 8 1.69626502 -0.54502419 9 -0.69264529 -1.61676626 10 0.52631934 -0.26636235 11 0.54617330 -1.52785395 12 0.06885748 0.79790822 13 1.21325507 1.36154991 14 -1.30941046 -2.13603119 15 0.87466151 -1.07515475 16 0.48641809 0.58819125 17 -1.06275086 -0.79261408 18 -0.59022569 0.08437131 19 -0.26362438 -1.31612747 20 -1.06148933 -0.36654012
- Related Articles
- How to convert a character data frame to numeric data frame in R?
- How to convert rows in an R data frame to list?
- How to convert NaN to NA in an R data frame?
- How to convert empty values to NA in an R data frame?
- How to convert a string in an R data frame to NA?
- How to convert NaN values to NA in an R data frame?
- How to convert number to words in an R data frame column?
- How to convert a data frame to data.table in R?
- How to convert alphabets to numbers in R data frame?
- How to convert columns of an R data frame into rows?
- How to convert negative values in an R data frame to positive values?
- How to create a new data frame for the mean of rows of some columns from an R data frame?
- How to convert a vector into data frame in R?
- How to convert a list to a data frame in R?
- How to convert data frame values to their rank in R?

Advertisements