20.6 C
London
Wednesday, August 13, 2025
HomeStatistics TutorialRR: Create New Data Frame from Existing Data Frame

R: Create New Data Frame from Existing Data Frame

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are two common ways to create a new data frame from an existing data frame in R:

Method 1: Select Column Names from Existing Data Frame

new_df var1', 'var3', 'var4')]

Method 2: Select & Rename Column Names from Existing Data Frame

new_df frame('new_var1' = df$var1, 
                     'new_var2' = df$var2, 
                     'new_var3' = df$var3)

The following examples show how to use each method with the following data frame in R:

#create data frame
df frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),
                 points=c(19, 14, 14, 29, 25, 30),
                 assists=c(4, 5, 5, 4, 12, 10),
                 rebounds=c(9, 7, 7, 6, 10, 11))

#view data frame
df

  team points assists rebounds
1    A     19       4        9
2    A     14       5        7
3    A     14       5        7
4    B     29       4        6
5    B     25      12       10
6    B     30      10       11

Example 1: Select Column Names from Existing Data Frame

The following code shows how to create a new data frame by selecting several column names from an existing data frame:

#define new data frame
new_df team', 'assists', 'points')]

#view new data frame
new_df

  team assists points
1    A       4     19
2    A       5     14
3    A       5     14
4    B       4     29
5    B      12     25
6    B      10     30

The new data frame contains three columns (team, assists, points) from the existing data frame.

Example 2: Select & Rename Column Names from Existing Data Frame

The following code shows how to create a new data frame by selecting and renaming several columns from an existing data frame:

#define new data frame
new_df frame('team_name' = df$team, 
                     'total_assists' = df$assists, 
                     'total_points' = df$points)

#view new data frame
new_df

  team_name total_assists total_points
1         A             4           19
2         A             5           14
3         A             5           14
4         B             4           29
5         B            12           25
6         B            10           30

The new data frame contains three columns (team, assists, points) from the existing data frame, but we have specified new names for each of the columns in the new data frame.

This approach is particularly useful if you know ahead of time that you want to rename the columns in the new data frame.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Append Rows to a Data Frame in R
How to Keep Certain Columns in R
How to Select Only Numeric Columns in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories