In this section, we will explore essential data manipulation tasks in R, including importing and exporting data, working with data frames, and performing data exploration.
R provides various functions to import data from different file formats, such as CSV, Excel, and databases. Some common functions include:
read.csv()
for reading CSV files.read.xlsx()
for reading Excel files.readRDS()
for reading R data files.Here’s an example of importing a CSV file:
# Importing data from a CSV file
data <- read.csv("data.csv")
To save your data or results, R offers functions like write.csv() for CSV files, write.xlsx() for Excel files, and saveRDS() for saving R data objects.
# Exporting data to a CSV file
write.csv(data, "output.csv")
Data frames are a common data structure in R for storing tabular data. They are like spreadsheets with rows and columns, and each column can have a different data type. You can create, manipulate, and explore data frames in R.
# Creating a data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 22),
Score = c(95, 88, 75)
)
df
## Name Age Score
## 1 Alice 25 95
## 2 Bob 30 88
## 3 Charlie 22 75
Data exploration involves understanding your data’s characteristics, such as summary statistics, distribution, and relationships between variables. You can use functions like summary(), str(), and data visualization libraries like ggplot2 for this purpose.
# Summary statistics
summary(df)
## Name Age Score
## Length:3 Min. :22.00 Min. :75.0
## Class :character 1st Qu.:23.50 1st Qu.:81.5
## Mode :character Median :25.00 Median :88.0
## Mean :25.67 Mean :86.0
## 3rd Qu.:27.50 3rd Qu.:91.5
## Max. :30.00 Max. :95.0
# Structure of the data frame
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ Name : chr "Alice" "Bob" "Charlie"
## $ Age : num 25 30 22
## $ Score: num 95 88 75
In this section, we’ve covered the basics of importing and exporting data and introduced data frames and data exploration. These skills are crucial for working with real-world data and performing meaningful analyses.
Feel free to practice these concepts with your own datasets to gain hands-on experience.
Free Lessons: