Open In App

R - Create Dataframe From Existing Dataframe

Last Updated : 19 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

R - Create DataFrame from Existing DataFrame means making a new data frame using the data already present in another data frame. We can do this by selecting specific columns, filtering rows or combining data. It helps in reusing and reshaping data for analysis. This can be done using base R, dplyr or data.table functions.

1. Using data.frame() with Direct Column Referencing

We create a new data frame and extract specific columns using the dollar sign operator.

  • data.frame(): Creates a new data frame.
  • df$column_name: Accesses specific columns from an existing data frame.
R
df <- data.frame(
  ID = 1:5,
  Name = c("Shravan", "Jeetu", "Lakhan", "Pankaj", "Mihika"),
  Age = c(20, 18, 19, 20, 18),
  Score = c(80, 75, 85, 90, 95)
)

new_df <- data.frame(
  Name = df$Name,
  Age = df$Age
)

print("New Data Frame (Direct Column Referencing):")
print(new_df)

Output:

dataframe
Output

2. Using subset() Function

We use subset() to extract specific columns from the data frame.

  • subset(): Extracts selected columns or rows from a data frame.
  • select = c(): Specifies which columns to include.
R
new_df_subset <- subset(df, select = c(Name, Score))

print("New Data Frame (Using subset()):")
print(new_df_subset)

Output:

dataframe
Output

3. Using merge() to Combine Two Data Frames

We merge two data frames using a common column.

  • merge(): Combines two data frames by common columns or row names.
  • by = "column": Defines the key column to merge on.
R
df1 <- data.frame(
  Name = c("Shravan", "Jeetu", "Lakhan", "Pankaj", "Mihika"),
  Age = c(20, 18, 19, 20, 18),
  Score = c(80, 75, 85, 90, 95)
)

df2 <- data.frame(
  Name = c("Shravan", "Jeetu", "Mihika"),
  Gender = c("Male", "Male", "Female")
)

merged_df <- merge(df1, df2, by = "Name")

print("Merged Data Frame:")
print(merged_df)

Output:

data_Frame
Output

4. Using Subset Method with Column Indexing

We select columns from a data frame using bracket notation.

  • [ , c()]: Selects specific columns using bracket notation.
R
new_df_bracket <- df[, c("Name", "Age")]

print("New Data Frame (Subset with Brackets):")
print(new_df_bracket)

Output:

data_Frame
Output

5. Using dplyr Package

We use the dplyr package to select columns in a clean way.

  • library(dplyr): Loads the dplyr package.
  • select(): Picks specified columns from a data frame.
R
library(dplyr)

new_df_dplyr <- select(df, Name, Score)

print("New Data Frame (Using dplyr):")
print(new_df_dplyr)

Output:

dataframe
Output

6. Using data.table Package

We use the data.table package for fast column selection and conversion.

  • library(data.table): Loads the data.table package.
  • as.data.table(): Converts a data frame to data.table format.
  • .(): Selects columns inside a data.table.
  • as.data.frame(): Converts the result back to a data frame.
R
library(data.table)

dt <- as.data.table(df)

new_df_dt <- as.data.frame(dt[, .(Name, Age)])

print("New Data Frame (Using data.table):")
print(new_df_dt)

Output:

data_frame
Output

The final output shows a new data frame with only the selected columns (Name and Age) using the data.table package, confirming that we successfully filtered the desired data.


Similar Reads