Importing CSV, Excel, and Text Files
Join our community on Telegram!
Join the biggest community of Pharma students and professionals.
In R, importing data from external files is one of the most common tasks. Real-world data is usually stored in formats such as CSV, Excel, or text files. R provides simple functions and packages that allow you to read these files and convert them into data frames or tibbles for analysis.
A CSV file (Comma Separated Values) is one of the most widely used data formats. Each line represents a row, and values are separated by commas. In R, CSV files are commonly imported using the read.csv() function from base R or the read_csv() function from the readr package. For example, data <- read.csv("data.csv") loads the CSV file into a data frame named data. If you are using the tidyverse approach, data <- read_csv("data.csv") creates a tibble with better printing and performance.
An Excel file usually has extensions such as .xls or .xlsx. To read Excel files in R, you typically use the readxl package. After installing and loading the package, you can use the read_excel() function. For example, data <- read_excel("data.xlsx") imports the Excel file into R. If the file contains multiple sheets, you can specify the sheet name or number inside the function.
A text file is a simple file that stores data in plain text format. Text files may use different separators such as tabs, spaces, or semicolons. In R, text files are commonly imported using the read.table() function. For example, data <- read.table("data.txt", header = TRUE) reads a text file where the first row contains column names. If the file is tab-separated, you can use read.delim().
Below is a table summarizing the common functions used for importing files in R:
| File Type | Function (Base R) | Function (Tidyverse) | Example |
|---|---|---|---|
| CSV | read.csv() |
read_csv() |
read.csv("data.csv") |
| Excel | Not in base R | read_excel() (readxl) |
read_excel("data.xlsx") |
| Text File | read.table() |
read_delim() |
read.table("data.txt") |
| Tab-Separated | read.delim() |
read_tsv() |
read.delim("data.txt") |
Before importing files, it is important to ensure that the file path is correct. You can set the working directory using setwd() or provide the full file path in the function. After importing, you can use functions like head() or str() to check the structure of the data.
Understanding how to import CSV, Excel, and text files is essential because most data analysis tasks begin with loading external data. Once the data is imported into R, you can clean, analyze, and visualize it using various R functions and packages.
