Thursday, 15 December 2016




Reading and Writing Data in R

Hello folks! In my previous post , how  to set up your working environment ad directories in R.
My following post is about how to read text files, load data sets in R and also correspondingly how to write the same.

So, here you go :

Reading tabular data in R :

There are multiple scenarios where we have to read data in the form of tables and we use these two variants of functions for the same :

>read.csv()

This function is exclusively used in reading csv files (comma separated files) in R

>read.csv(file, header = TRUE, sep = ",", quote = "\"", nrows=100
         dec = ".", fill = TRUE,colClasses=c("factor","decimal"),stringsAsFactors=TRUE)

file : Provide a file path or a filename if you have already set your working directory.

header : If this field is set to TRUE , it means the file has headers included . By default, it is set to TRUE

nrows : mentions the number of rows to be loaded into the dataset

sep : It is comma by default for read.csv() function but for functions like read.table , a separator needs to be specified explicitly. 

quote : To disable quoting altogether, use quote = ""

dec : character used in file for decimal points

colClasses : It used to specify the type class of the column vector

Tip : use colClasses  as it processes faster in R

# How to know the classes of columns easily ?

initial= read.csv("file.csv",nrows=100)
classes=sapply(initial,class)
tabAll=read.table("file.csv",colClasses=classes)

>read.table()

This function is used to read in tabular data in R, given that separator is a white space by default.

It has similar arguments as read.csv() . 

> readLines() : It is used when one wants to read lines from a connection

>source()  and dget() : 
> source() :

For reading in R source Code AND METADATA gets stored

>load()

load() is used to read in saved workspace

>unserialize()

For reading single R  objects in binary form/binary objects

Writing files from R 

It is relatively easy to remember functions in R if once someone has learnt how to read files in R.

These functions occur in pairs , let's see :

>write.csv(dataset,file,rownames=FALSE)

dataset : the dataset we wish to write 

file : path or filename if working directory is set

rownames : R dataframes use rownames to index rows , they should explicitly be set to FALSE to avoid unrequired column

Similarily we have syntax for write.table()

The counterpart of source() is dump() and of dget() is dput() 

The difference lies in the number of data frames which dump can write are multiple wherein dput allows only single object to be written :

For instance :

dump vs dput 

>dump(c("y,"k),"testh.csv")

>source("testh.csv")

data is often stored in this format :

y <-
structure(list(a = 1 b = structure(1L .Label = "y" class = "factor")) .Names = c("a"
b) row.names = c(NA -1L) class = "data.frame")
k <-
structure(list(v = structure(1L .Label = "hi" class = "factor")) .Names = "v" row.names = c(NA
-1L) class = "data.frame")

Finally , we have save() function used to save workspaces in R.

>save( file = "/Rdata.rda")


This is it for reading and writing of files, you may come across newer arguments and functions, all you have to do is check ?func() and the help tab shall provide you the neccessary details .

Eventhough, we have now learnt how to read and write data in R, it is imperative to judiciously use memory and estimate before hand , how much RAM is our dataset going to occupy.

My next post shall cover how one can estimate memory occupied by our data in advance. Stay tuned!







No comments:

Post a Comment