Data Frames in R Programming
1. Structure of Data Frames
Data frames are used to store tabular data in R. They can hold different data types in each column.
# Creating a data frame
my_data_frame <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Gender = c("F", "M", "M")
)
# Displaying the structure of the data frame
str(my_data_frame)
# Printing the data frame
print(my_data_frame)
2. Accessing Columns
Columns in a data frame can be accessed using the column name or index.
# Accessing a column by name
print(my_data_frame$Name)
# Accessing a column by index
print(my_data_frame[, 1])
3. Filtering Rows
Rows can be filtered based on conditions.
# Filter rows where Age is greater than 28
filtered_data <- my_data_frame[my_data_frame$Age > 28, ]
print(filtered_data)
4. Adding Rows and Columns
Adding Rows
# Adding a new row
new_row <- data.frame(Name = "David", Age = 40, Gender = "M")
my_data_frame <- rbind(my_data_frame, new_row)
print(my_data_frame)
Adding Columns
# Adding a new column
my_data_frame$Score <- c(85, 90, 95, 80)
print(my_data_frame)
5. Removing Rows and Columns
Removing Rows
# Removing a row by index
my_data_frame <- my_data_frame[-2, ]
print(my_data_frame)
Removing Columns
# Removing a column
my_data_frame$Score <- NULL
print(my_data_frame)
Conclusion
This tutorial explained the structure of data frames in R, how to access columns, filter rows, and add or remove rows and columns. Data frames are essential for managing and analyzing tabular data in R.