Read and Write Data in R | Beginning R Programming – Part 5

It goes without saying that in data science you will often be reading in and writing out data in R. Learn how to read a data file as a data frame and write out a data frame to a file. R has several built-in interfaces for text data reading, writing, and understanding how to utilize these will add valuable tools to your R toolset. Check out how our data science bootcamp can help you reinforce this skill.

When learning R for data science it
goes without saying that you’ll need to
learn how to read in and write out Data
Data often stored in comma separated
file format so we’ll learn how to read
and write data in this format but the
functions are syntax for reading data
in other formats are very similar
so here is our income data set which
is a comma separated file that
sits in our Documents folder
what we want to do is
read in this data in a simple single
command line and this data set is
accompanying this video you can find it
on the learning portal
so we’ll give our data set a variable name
and we’ll just simply call it income
then we’ll use the read CSV function
and we give this function the path
or the folder directory of where our data sits
so in my case it’s within users
and after the forward slash I can simply tab
and navigate my way there
otherwise you’re welcome to type this out by hand
now I have headers in my columns
but if I didn’t I could always
set header equals false
as I do have headers it
will automatically read this in and
it also infer the data types as well
Okay, now that we have given it the
full path with quotation marks around
the string let’s run this line
and we can use the head function here
to see if it’s read it…read the data incorrectly
so the head function just gives us the
first few rows of data
we have just read in our data as a data frame
we’ll explain more what we
mean by data frame later on
In Windows you need to give it the full path or
directory you need to use kind of double
backslashes windows interpret this as
the path so for example inside the read
CSV function you’d simply just use double
and it usually sits in your C Drive
Now another way you can read in this data set
is instead of like giving
the read CSV function the full path
we could set up a working directory
to where all our files are
the files that we’ll
be working with so then we only
have to use the filename and extension
when reading in the data set
So for example I’ll use the set WD
function here
and I’ll give it my working directory
so everything I’m gonna use
sits within documents
and so when I read in my data here
I’ll only really need to
use the filename and extension
making life a little bit more easier
so you can also
read in a data set using Rstudio
if you like
so this bottom right panel here
if you look under the files tab you’ll
see documents where our data is stored
just click on this and then double click
on this and import data set and that’s
another way you can read it into R
now to write out data
we use the write CSV function
you might want to write out the
entire data set so
we just give it the name of our data set
or it could be a variable
within our data set so the
outcome variable or the predictions
column for example maybe you want to
write that to a CSV file
and we have to give it the
directory to where we want
to store our CSV file and a name of
our CSV file as well so I’ll just put it
in the same place
it’s been putting everything else
and I’m just gonna call it
We might not want to include
row numbers or the index
as a column in our CSV file
if that’s the case we can always
set row names equals false
okay let’s run this
and let’s have a look
okay great so as you can see
we have successfully written data to a CSV file
In the next video we’ll discuss data frames

Download the Data Set
How to Install R

Part 6: 
Data Frames

Part 4: 
Operators: Arithmetic, Rational, and Logical

Full Series:
Beginning R Programming

More Data Science Material:
[Video] Scaling R to Big Data using Hadoop and Spark
[Blog] Hadoop Tutorial: Large Dataset Resample
[Blog] Making Your Data Science Skills Marketable


Rebecca Merrett
About The Author
- Rebecca holds a bachelor’s degree of information and media from the University of Technology Sydney and a post graduate diploma in mathematics and statistics from the University of Southern Queensland. She has a background in technical writing for games dev and has written for tech publications.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>