Introduction
Selecting specific columns from a data frame is a common task in data manipulation and analysis. This process allows you to focus on the relevant data, simplifying further analysis. This guide will walk you through writing an R program that selects specific columns from a data frame.
Problem Statement
Create an R program that:
- Creates an initial data frame.
- Selects specific columns from the data frame.
- Displays the selected columns.
Example:
- Input: A data frame with
Name,Age,Gender, andScorecolumns. Select theNameandScorecolumns. - Output: A data frame containing only the
NameandScorecolumns.
Solution Steps
- Create the Initial Data Frame: Use the
data.frame()function to create a data frame. - Select Specific Columns: Use indexing to select specific columns from the data frame.
- Display the Selected Columns: Use the
print()function to display the selected columns.
R Program
# R Program to Select Specific Columns from a Data Frame
# Author: Ramesh Fadatare
# Step 1: Create the initial data frame
names <- c("Ramesh", "Suresh", "Mahesh", "Ganesh", "Rajesh")
ages <- c(25, 30, 22, 28, 24)
genders <- c("Male", "Male", "Male", "Male", "Male")
scores <- c(85.5, 90.0, 88.5, 92.0, 80.0)
students_data <- data.frame(Name = names, Age = ages, Gender = genders, Score = scores)
# Step 2: Select specific columns from the data frame
selected_data <- students_data[, c("Name", "Score")]
# Step 3: Display the selected columns
print("Selected Columns (Name and Score):")
print(selected_data)
Explanation
Step 1: Create the Initial Data Frame
- The data frame
students_datais created using thedata.frame()function, with columns forName,Age,Gender, andScore.
Step 2: Select Specific Columns
- The data frame is subset using the indexing
students_data[, c("Name", "Score")]to select only theNameandScorecolumns. - The selected columns are stored in the
selected_datavariable.
Step 3: Display the Selected Columns
- The
print()function is used to display the data frame containing only the selected columns.
Output Example
Example:
[1] "Selected Columns (Name and Score):"
Name Score
1 Ramesh 85.5
2 Suresh 90.0
3 Mahesh 88.5
4 Ganesh 92.0
5 Rajesh 80.0
Conclusion
This R program demonstrates how to select specific columns from a data frame. It covers essential operations such as creating a data frame, selecting columns using indexing, and displaying the results. Selecting specific columns is a fundamental operation in data manipulation, allowing you to focus on the most relevant data for your analysis. This example is particularly useful for anyone learning how to manipulate and analyze data frames in R.