Reading and Writing Data in R: A Quick Guide

For dealing with data, one may need “reading and writing data in R”. Therefore, it is important to discuss reading and writing data in R. It is also important to learn about reading and writing data files in the R environment.

Reading and Writing Data in R Language

Reading Data in R

  • read.table(), and read.csv(), for reading tabular data
  • readLines() for reading lines of a text file
  • source() for reading in R code files (inverse of dump)
  • dget() for reading in R code files (inverse of dput)
  • load() for reading in saved workspaces.

Writing Data to File in R

Following are a few functions for writing (exporting) data to files in R Language.

  • write.table() in R, and write.csv() export data to a wider range of file formats including CSV and tab-delimited.
  • writeLines() write text lines to a text-mode connection.
  • dump() takes a vector of names of R objects and produces text representations of the objects on a file (or connection). A dump file can usually be sourced into another R session.
  • dput() writes an ASCII text representation of an R object to a file (or connection) or uses one to recreate the object.
  • save() writes an external representation of R objects to the specified file.

Reading Data Files with read.table()

The read.table() function in R is one of the most commonly used functions for reading data into R. It has a few important arguments.

  • file, the name of a file, or a connection
  • header, logical indicating if the file has a header line
  • sep, a string indicating how the columns are separated
  • colClasses, a character vector indicating the class of each column in the data set
  • nrows, the number of rows in the dataset
  • comment.char, a character string indicating the comment character
  • skip, the number of lines to skip from the beginning
  • stringsAsFactors, should character variables be coded as factors?

read.table() and read.csv() Examples

data <- read.table("foo.txt")
data <- read.table("D:\\datafiles\\mydata.txt")
data <- read.csv("D:\\datafiles\\mydata.csv")

R will automatically skip lines that begin with a symbol # and figure out how many rows there are (and how much memory needs to be allocated). R also figures out what type of variable is in each table column.

Writing Data Files with write.table() in R

A few important arguments are usually used in function write.table() in R.

  • x, the object to be written, typically a data frame
  • file, the name of the file that the data are to be written to
  • sep, the field separator string
  • col.names, a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written
  • row.names, a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written
  • na, the string to use for missing values in the data

write.table() and write.csv() Examples

The following are few examples about saving data in CSV or TXT files.

x <- data.frame(a = 5, b = 10, c = pi)

write.table(x, file = "data.csv", sep = ",")
write.table(x, "c:\\mydata.txt", sep = "\t")
write.csv(x, file = "data.csv")
Rfaqs: Reading and Writing Data in R

Importing and exporting data in R

SPSS Data Analysis

List in R Language: A Comprehensive Guide

In R language, a list is an object that consists of an ordered collection of objects known as its components. A list in R Language is structured data that can have any number of modes (types) or other structured data. That is, one can put any kind of object (like vector, data frame, character object, matrix, and/ or array) into one list object. An example of a list is

x <- list(c(1,2,3,5), c("a", "b", "c", "d"), c(T, T, F, T, F), matrix(1:9, nr = 3) )

that contains 4 components, three of them are vectors (numeric, string, and logical) and one of them is a matrix.

List in R Language

Converting Objects to List in R Language

An object can also be converted to a list by using the as.list( ) function. For vector, the disadvantage is that each element of the vector becomes a component of that list. For example,

as.list (1: 10)

Extract components from a list

The operator [[ ]] (double square bracket) is used to extract the components of a list. To extract the second component of the list, one can write at R prompt,

list[[2]]

Using the [ ] operator returns a list rather than the structured data (the component of the list). The component of the list need not be of the same mode. The components are always numbered. If x1 is the name of a list with four components, then individual components may be referred to as x1[[1]], x1[[2]], x1[[3]], and x1[[4]].

If components of a list are defined then these components can be extracted by using the names of components. For example, a list with named components is

x1 <- list(a = c(1,2,3,5), b = c("a", "b", "c", "d"), c = c(T, T, F, T, F), 
d = matrix(1:9, nr = 3) )

To extract the component a, one can write

x1$a
x1["a"]
x1[["a"]]

To extract more than one component, one can write

x[c(1,2)]     #extract component one and two
x[-1]         #extract all component except 1st
x[[c(2,2)]]   #extract 2nd element of component two
x[[c(2:4)]]   #extract all elements of component 2 to 4
Practicing R for Statistical Computing: rfaqs.com

https://itfeature.com

R Objects, Workspace, and .RData file

The post is about an introduction to workspace, R objects, and .Rdata file in R language.

R Language as Functional Language

The R program’s structure is similar to the programs written in other computer languages such as C or its successors C++ and Java. However, important differences between these languages and R are (i) R has no header files, (ii) most of the declarations are implicit, (iii) there are no pointers in R, and (iv) text and strings as vectors can be defined and manipulated directly.

R is a functional language. Most of the computation in R is handled using functions. The R language environment is designed to facilitate the development of new scientific computation tools.

R Objects

Everything (such as functions and data structure) in R is an object. To see the names of all R objects in the workspace, on the R command prompt just type,

ls()

objects() is an alternative to ls() function. Similarly, typing the name of any object on the R prompt displays (prints) the content of that object. As an example type q, mean, and lm, etc. on the R prompt.

R Workspace

It is possible to save individual objects or collections of objects into a named image file. The named image file has an extension of .RData. Some possibilities to save an object from R workspace are:

To save the content of R workspace into a file .RData, type

save.image()

To save objects in the file archive.RData, type

save.image(file = "archive.RData")

To save some required objects in data.RData, type

save(x, y, file = "data.RData")

These image files can be attached to make objects available in the next R session. For example.

attached ("arvhive.RData")
R workspace, R Objects

Note that when quitting, R offers the option of saving the workspace image. By default, the workspace is saved in an image file (.RData) in the working directory. The image file can be used in the next R session. Saving the workspace image will save everything from the current workspace. Therefore, use the rm() function to remove objects that are not further required in the next R session.

For further details about saving and loading R workspace visit: Save and Load R Workspace

Learn Statistics and Data Analysis