Whether you’re a beginner or a data science professional, working with data requires a basic understanding of common data types, querying data, and converting data from one format to another. R offers several facilities for import and export. We’ve assembled a collection of articles talking about how to read and write data to and from R, particularly in non-standard formats.
The first step to import data from a text file is to read the file using a file reader. R offers several different file readers. The most familiar is the file. File readers accept a filename as a parameter and will read the file using the filename. The file can be read in either text or binary mode.
Common Requests
- Reading CSV Files and reading CSV’s from the web
- Web Scraping with rvest
- Web Scraping Json Sources
- SAS Files
- The fread function in R (part of data.tables)
- Reading .sav files
- How to read .dat files
- Reading Fixed width text files in R
- How to read a .zip file without unzipping it
R offers an additional type of file connection, called a pipe. This type of connection can be opened for reading, writing, and appending. When you open a pipe connection, the OS will run a command and make standard output available for R input. If you do not want to run the OS command, you can also open the file connection in binary mode.
R’s readBin function reads a stream of bytes from the file and interprets it as a vector of mode. If you use an omitted size argument, the function will read the entire file. The second argument can be a number, an atomic vector object, a character, or an object of a specified type. You can also use the typeof() function to confirm the type of data.
To export data, you can either export a single R object using the Rds function, or you can export multiple R objects using the Rdata function. Rdata is sometimes shortened to Rda. You can also use the function url as an input url to a text connection.