Adventures in Machine Learning

Mastering CSV and Text File Importing and Exporting in R

As data-driven professionals, importing files is a daily task in our work. R, a popular programming language for data analysis, offers numerous functions to import data in various formats.

However, for newcomers, learning how to import data in R might seem daunting. This article aims to ease that process by providing a comprehensive guide on how to import CSV and text files into R, accompanied by examples to solidify your understanding.

Importing a CSV File Into R

CSV or comma-separated values is a popular file format for data import because it’s easy to read, create, and manage. Here is a template for importing a CSV file:


dataframe_name <- read.csv("file_location/file_name.csv", header = TRUE)


The `read.csv` function is a built-in function in R that reads CSV files.

The function works by taking two arguments, the file location and the file name. The command `dataframe_name` is used to store the result of `read.csv` into a data frame object.

Here is an example of importing a CSV file and modifying the path name:



file_path <- "C:/Documents/Data/file.csv"

dataframe <- read_csv(file_path)


In this example, we use the `readr` package’s `read_csv` function to import the CSV file. The `file_path` object stores the file location and the `read_csv` function reads the file and stores it in the `dataframe` object.

Importing a Text File into R

Text files are frequently used in data analysis because they allow data to be imported and exported in a user-friendly, readable format. To import a text file into R, we must first change its file extension from CSV to TXT.

We can use the following command to read a text file:


dataframe_name <- read.table("file_location/file_name.txt", header = TRUE)


The `read.table` function is a built-in function in R that reads text files. The function works similarly to `read.csv`, where the first argument is the file location and file name, and the second argument is a logical value that tells R whether the file contains headers.

Changing the file extension from CSV to TXT

Let’s say we have a CSV file named `example.csv`, and we want to change it to a text file with the extension `.txt`. We can use the following code:


read.csv(“example.csv”) ->write.table(“example.txt”,sep=”t”, row.names = FALSE, col.names = TRUE)


This command reads the content of the CSV file and then writes a text file with the `.txt` extension to the working directory.

The `sep` argument specifies the separator used in the text file, and `row.names` and `col.names` are logical arguments that specify whether or not to include row and column names.


Importing different file formats is a fundamental skill in R data analysis. By providing a comprehensive guide on how to import CSV and text files into R, and examples that illustrate the process, we have demystified the process and made it more accessible to beginners.

In conclusion, importing CSV and text files into R is relatively easy, once you know the proper syntax and commands, and have a basic understanding of R programming. In the previous section, we covered how to import CSV and text files into R.

Still, before diving into the next section, it’s essential to understand the `read.csv` and `read.table` functions extensively. Let’s discuss the `read.csv` function and its associated documentation:

Link to `read.csv` documentation:

According to the documentation, the `read.csv` function reads a file in comma-separated format and returns a data frame.

It has several arguments, including:

1. `file`: the name of the file or a connection to the file.

2. `header`: a logical value indicating whether the file contains a header line with column names.

3. `sep`: the separator character used in the file.

By default, it’s a comma. 4.

`dec`: the character used for decimal points. By default, it’s a period.

5. `fill`: a logical value indicating whether to fill in any missing values with NAs.

For example, suppose we have a CSV file named `sales.csv` with the following data:







To read the file and store it in a data frame named `sales_df`, we can use the following command:


sales_df <- read.csv("sales.csv", header = TRUE)


This will create a data frame with three columns: `Date`, `Sales`, and `Expenses`.

Now that we understand how to import data into R let’s discuss how to export data to a CSV file in R.

Exporting Data to a CSV File in R

Exporting data in R is a straightforward process, thanks to the built-in `write.csv` function. The function takes two arguments, the data frame object name and the file path and name.

It produces a CSV file with the data frame object’s contents. Here’s an example:


dataframe_name <- data.frame(

column1 = c(“apple”, “banana”, “orange”),

column2 = c(1, 2, 3)


write.csv(dataframe_name, “dataframe_name.csv”, row.names = FALSE)


In this example, we created a data frame named `dataframe_name` with two columns, `column1` and `column2`.

We then used the `write.csv` function to write the contents of the data frame to a CSV file named `dataframe_name.csv`. The `row.names` argument specifies whether or not to include row names in the CSV file.

By default, it’s set to `TRUE`, so we have to set it to `FALSE` if we want to exclude them. It’s also worth noting that we don’t necessarily have to create a new data frame to export data.

We can export subsets of existing data frames, like so:



subset <- mtcars[, c("mpg", "cyl")]

write.csv(subset, “mtcars_subset.csv”, row.names = FALSE)


This will create a new CSV file named `mtcars_subset.csv` that contains only the `mpg` and `cyl` columns of the `mtcars` data frame.


In conclusion, importing and exporting data in R is simple and straightforward. We can use two built-in R functions, `read.csv` and `write.csv`, to import and export data, respectively.

When using these functions, it’s important to keep in mind their syntax, arguments, and options. Understanding these functions is crucial for data analysis, and we can refer to their documentation for more detailed information.

In conclusion, knowing how to import and export CSV and text files in R is a must-have skill for data-driven professionals. The `read.csv`, `read.table`, and `write.csv` functions are built-in, easy-to-use tools in R that allow us to read and write data with minimal effort.

To recap, we covered the templates for importing CSV and text files, the process for changing a CSV file into a text file format, the `read.csv` documentation, and how to export data to a CSV file. By mastering these skills, we can significantly improve our productivity in data analysis.

Importing and exporting data is a crucial step in the data analysis process that sets the foundation for performing further manipulations and modeling. Let’s continue to improve our data analysis skills by learning new tools and techniques to take our abilities to the next level.

Popular Posts