A data frame helps you organize data in rows and columns, like a table. It makes it easy to work with and analyze data.
0
0
Data frame creation in R Programming
Introduction
You want to store survey results with different types of answers.
You need to combine several lists of related information into one table.
You want to prepare data for plotting or statistics.
You have data from different sources and want to compare them side by side.
Syntax
R Programming
data_frame_name <- data.frame(column1 = c(value1, value2), column2 = c(value3, value4))
Each column is created by naming it and giving a vector of values.
All columns should have the same length to form a proper table.
Examples
This creates a data frame with two columns: 'name' and 'age'.
R Programming
df <- data.frame(name = c("Alice", "Bob"), age = c(25, 30))
Here, 'product' and 'price' columns hold product names and their prices.
R Programming
df <- data.frame(product = c("Pen", "Book"), price = c(1.5, 10))
This example shows a data frame with three rows and two columns.
R Programming
df <- data.frame(city = c("NY", "LA", "SF"), population = c(8000000, 4000000, 900000))
Sample Program
This program creates a data frame called 'people' with three columns: Name, Age, and Height. Then it prints the table.
R Programming
names <- c("John", "Jane", "Jim") ages <- c(28, 34, 29) heights <- c(175, 160, 180) people <- data.frame(Name = names, Age = ages, Height = heights) print(people)
OutputSuccess
Important Notes
Data frames can hold different types of data in each column, like numbers and text.
Use str(data_frame_name) to see the structure of your data frame.
Column names should be unique and meaningful for easier understanding.
Summary
Data frames organize data in rows and columns like a spreadsheet.
Create data frames by combining vectors of equal length with data.frame().
They are useful for storing and analyzing mixed types of data together.