R Data Frames

Introduction

In this chapter, you will learn about data frames in R. Data frames are two-dimensional data structures that can hold elements of different types (numeric, character, logical) in each column. They are similar to tables in a database or Excel spreadsheets and are essential for data manipulation and analysis in R.

Creating Data Frames

Data frames can be created using the data.frame() function, which combines vectors of equal length into a data frame.

Example: Creating a Data Frame

Example:

# Creating a data frame df <- data.frame( Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 35), Gender = c("F", "M", "M") ) print(df) 

Accessing Elements in Data Frames

You can access elements in a data frame using the $ operator, square brackets [], or the subset() function.

Example: Accessing Elements in a Data Frame

Example:

# Accessing columns using the $ operator print(df$Name) # Output: "Alice" "Bob" "Charlie" print(df$Age) # Output: 25 30 35 # Accessing elements using square brackets print(df[1, ]) # First row print(df[, 2]) # Second column print(df[2, 3]) # Element at second row, third column # Using subset() function print(subset(df, Age > 25)) 

Modifying Elements in Data Frames

You can modify elements in a data frame by specifying the row and column indices or by using the $ operator.

Example: Modifying Elements in a Data Frame

Example:

# Modifying elements df$Age[2] <- 32 df[3, "Gender"] <- "F" print(df) 

Adding and Removing Columns

You can add columns to a data frame by assigning a new vector to a new column name. You can remove columns by setting them to NULL.

Example: Adding and Removing Columns

Example:

# Adding a column df$Salary <- c(50000, 55000, 60000) print(df) # Removing a column df$Salary <- NULL print(df) 

Data Frame Operations

Example: Basic Operations

Example:

# Summary statistics print(summary(df)) # Number of rows and columns print(nrow(df)) # Output: 3 print(ncol(df)) # Output: 3 # Structure of the data frame print(str(df)) # Names of the columns print(names(df)) 

Merging and Combining Data Frames

You can merge data frames using the merge() function and combine them using the rbind() and cbind() functions.

Example: Merging and Combining Data Frames

Example:

# Creating another data frame df2 <- data.frame( Name = c("Alice", "Bob", "David"), Department = c("HR", "Finance", "IT") ) # Merging data frames by a common column merged_df <- merge(df, df2, by = "Name") print(merged_df) # Combining data frames by rows combined_df_rows <- rbind(df, df2) print(combined_df_rows) # Combining data frames by columns combined_df_cols <- cbind(df, df2) print(combined_df_cols) 

Example Program Using Data Frames

Here is an example program that demonstrates the use of data frames in R:

# R Program to Demonstrate Data Frames # Creating a data frame df <- data.frame( Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 35), Gender = c("F", "M", "M") ) print("Data Frame:") print(df) # Accessing elements print("Name Column:") print(df$Name) print("First Row:") print(df[1, ]) print("Second Column:") print(df[, 2]) print("Element at (2, 3):") print(df[2, 3]) # Modifying elements df$Age[2] <- 32 df[3, "Gender"] <- "F" print("Modified Data Frame:") print(df) # Adding and removing columns df$Salary <- c(50000, 55000, 60000) print("Data Frame with Added Column:") print(df) df$Salary <- NULL print("Data Frame with Removed Column:") print(df) # Data frame operations print("Summary of Data Frame:") print(summary(df)) print("Number of Rows and Columns:") print(nrow(df)) print(ncol(df)) print("Structure of Data Frame:") print(str(df)) print("Column Names:") print(names(df)) # Merging and combining data frames df2 <- data.frame( Name = c("Alice", "Bob", "David"), Department = c("HR", "Finance", "IT") ) merged_df <- merge(df, df2, by = "Name") print("Merged Data Frame:") print(merged_df) combined_df_rows <- rbind(df, df2) print("Combined Data Frame by Rows:") print(combined_df_rows) combined_df_cols <- cbind(df, df2) print("Combined Data Frame by Columns:") print(combined_df_cols) 

Conclusion

In this chapter, you learned about data frames in R, including how to create, access, modify, and perform operations on data frames. You also learned how to merge and combine data frames. Data frames are essential data structures for data manipulation and analysis in R. By mastering data frames, you can efficiently handle and analyze complex datasets in your R programs.

Leave a Comment

Scroll to Top