---
title: "Importing Data with `read.csv` and `readxl` Packages"
author: "Valerio Licursi"
date: "2024-06-26"
output: html_document
---

# Introduction

This exercise will help you learn how to import data into R using the `read.csv` function from base R and the `read_excel` function from the `readxl` package. These are common methods for importing CSV and Excel files, respectively.

## Prerequisites

-   `readxl` package installed (`install.packages("readxl")`)

# Step 1: Importing CSV Data with `read.csv`

## 1. Create a Sample CSV File

First, we create a sample CSV file to work with.

```{r create-csv}
write.csv(iris, "iris_sample.csv", row.names = FALSE)
```

## 2. Import the CSV File

Use the read.csv function to import the CSV file.

```{r import-csv}
iris_csv <- read.csv("iris_sample.csv")
head(iris_csv)
```

# Step 2: Importing Excel Data with readxl

## 1. Create a Sample Excel File

We need to create a sample **Excel** file. This requires the writexl package, which can be installed using `install.packages("writexl")`.

```{r writexl}
# install.packages("writexl")
library(writexl)
write_xlsx(iris, "iris_sample.xlsx")
```

## 2. Import the Excel File

Use the read_excel function from the readxl package to import the Excel file.

```{r read-excel}
library(readxl)
iris_excel <- read_excel("iris_sample.xlsx")
head(iris_excel)
```

# Step 3: Comparing Imported Data

Compare the first few rows of the data imported from the CSV and Excel files to ensure they match.

```{r comparing-data}
head(iris_csv)
head(iris_excel)
```

# Step 4: Cleaning Up

Remove the sample files created for this exercise.

```{r cleaning}
file.remove("iris_sample.csv", "iris_sample.xlsx")
```

### Exercise for you: import the `rnaseq.xlsx` file:

```{r}

```

---

# References

-   [R for Data Science by Hadley Wickham & Garrett Grolemund](https://r4ds.had.co.nz/)

-   readxl package documentation
