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:
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))
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…