Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

1. R Basics > Loading a Delimited Text Data File

Loading a Delimited Text Data File

Problem

You want to load data from a delimited text file.

Solution

The most common way to read in a file is to use comma-separated values (CSV) data:

data <- read.csv("datafile.csv")

Discussion

Since data files have many different formats, there are many options for loading them. For example, if the data file does not have headers in the first row:

data <- read.csv("datafile.csv", header=FALSE)

The resulting data frame will have columns named V1, V2, and so on, and you will probably want to rename them manually:

# Manually assign the header names
names(data) <- c("Column1","Column2","Column3")

You can set the delimiter with sep. If it is space-delimited, use sep=" ". If it is tab-delimited, use \t, as in:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint