Categories: Data Science

Learn R – How to Create Data Frames using Existing Data Frame

This article represents commands that could be used to create data frames using existing data frame. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following is a list of command summary for creating data frames by extracting multiple columns from existing data frame based on following criteria, whose sample is provided later in this article:

  • Column indices
  • Column names
  • Subset command
  • Data.frame command
6 Techniques for Extracting Data Frame from Existing Data Frames

Following commands have been based on diamonds data frame which is loaded as part of loading ggplot2 library.

 

Following is how the diamonds data frame looks like:

#1: Create data frame with selected columns using column indices
# Displays column carat, cut, depth
dfnew1 <- diamonds[,c(1,2,5)]

#2: Create data frame with selected columns using column indices with sequences
# Displays column carat, cut, color, depth, price, x
dfnew2 <- diamonds[, c(1:3, 5, 7:8)]

#3: Create data frame with selected columns using data.frame command
# Displays column carat, cut, color
dfnew3 <- data.frame(diamonds$carat, diamonds$cut, diamonds$color)
names(dfnew3) <- c("Carat", "Cut", "Color")

#4: Create data frame using selected columns and column names
# Displays column carat, depth, price
dfnew4 <- diamonds[,c("carat", "depth", "price")]

#5: Create data frame using subset command and column names
# Displays column color, carat, price
dfnew5 <- subset(diamonds, select=c("color", "carat", "price"))

#6: Create data frame using subset command and column indices
# Displays column carat, cut, color, depth
dfnew6 <- subset(diamonds, select=c(1:3, 5))
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

24 hours ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

2 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

2 days ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

3 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

3 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

3 days ago