Hello and welcome to another video for programming in R. In this video we will be taking a look at how to read in and write out
data using the read.table and write.table functions.
So let's start by taking a look at the read.table function.
So we can see from the help menu that first we're going to have indicate file. Now file is going to correspond
to the file path, the file name, as well as the .file type.
We're also going to have to see if we have headers or not in our file. By default, set to false, so we can skip this if we notice that
there are no headers in our file. There is also this "sep" option which is for the separator or delimiter.
And we can see by default it is going to be set to a space.
If its something different, then we'll have to change it too would ever type is necessary;
and there some other options as well but we're just going to be focusing on the first three.
So let's now take a look at the file and see if we can get some of this information. Now the
example that I have here is just sitting in my R_is_Awesome folder which is on my desktop.
But let's first take a look at inside this file. Now looking at the file we can see that there doesn't look to be any headers and that each
piece of information here is separated
by a comma. To get our other information let's close the file
and take a look at some of the properties here.
You can see that this is can be a .txt or a text file.
And the location here, the path to this file, is also shown. So I'm just going to copy this, make typing a little easier,
close that out, return over here, and now let's use the function to read in the data. So I'm just going to use a generic dat,
this is what I want to save it as,
to call on read.table,
and then the first piece will be the file. You'll want to make sure that you put this file path in parentheses or single quotes.
every time. And the other thing you'll have to make sure that you do in R especially with a PC is that you have
these slashes turned the other way.
And the reason for that is that R is going to recognize that as an escape character
when it comes to strings; so it's going to be really confused.
So there's the file path, now we also have to indicate the name of the file; 'airports'.
and was a .txt. You have to make sure that you put in the file type as well.
Alright, the next piece; well we can skip the header because by default it was false.
The next piece is gonna be the separator, and remember that it was a comma. You also want to make
sure that you put that into either single or double brackets.
You'll notice that it populated 'dat' into our work environment,
and if we take a look inside it looks like over 8,000 observations. It might seem like that's a lot, but this is really a smaller size
file which this function can handle.
Now if it was a very, very large
type of file you're probably going to want to use a more efficient function from a package, but for this it's sufficient. We can also
view the contents inside and kinda
give it a look over to make sure everything appears to be reading in correctly. It looks like there are some missing bits of information
but that hasn't kind of mixed everything up, so everything looks to be pretty good.
Let's take a look at writing out a dataset.
And go on ahead and copy and
paste that up here into the editor. Let's say that after we read this in we do something
to this
dataset. And then we want to save that result so just to something simple; I'll call it dat.out, and lets say that it's just a subset
of this dataset.
So lets say I wanted all rows
fourth and fifth columns
and lets take a look to see if that looks like that makes sense and everything looks pretty good
So lets say that we want to write this out. So returning over here to help menu
we put in write.table
we'll see that first piece here is "x". So "x" is going to be the object which is going to be written out
the next piece is going to be the same file component. Except now it's going be where do you write it out to, and the
other couple things here we're not going to focus on, but the separator piece is something we will take a look at again. Something else
I want to make mention of here is that whenever you use the read.table that it's going to be writing in
the dataset as a data frame, and we can see this if I return back to data.table
and see that it would read it in and creates a data frame. Okay, so lets call upon
write.table function
and we're gonna write out
dat.out
We use that same file path but we're gonna want to make sure that we change the name, right? Because if we didn't then we'd
just be writing right over top of our original dataset which wouldn't be a very good thing. And lets just call it subset.
Lets also say that the
we don't want to save it as a txt file. Let's say that we're going to save it as a csv, right?
Just another format. If you're going to use a csv file format then, of course, you're going to want the separator to be a comma. So if we go
on ahead and write that out. And we go back to our folder
we'll see that now this new file has been written out. And if we open this up
we're going to see that it doesn't quite look the way that we wanted it to, right? Instead of having just those two columns
it looks like we have these
two headers, but now we have this column of just
numbers here and that's because we forgot to indicate that we did not want
the row names which were just these numbers running down the side of it.
So let's go on ahead and fix that.
And we'll fix that by
taking a look at the options here
and notice that there's this row.names = True by default.
We'll go back in here
and say maybe that's looking a little long, so I'll go down to the next line because R is going to ignore
any spacing as long as you're within the function call. We'll say row.names
equals false.
We're going to see that it comes up with an error.
and that's because I still have the file open,
so close that file.
Then run it, comeback, open it up, and we'll see that now everything
appears to be the way it should be even with the space in here. Now, this isn't the only file type
you can write out as. We could keep that as a txt
and we could for some reason use just a space
that will work as well.
We write this out
we'll see that now there's this other file that's been created, another subset, but it's a different file format so it didn't replace the other
one. If we open up we can see that now it is space separated rather than comma
separated. So that concludes this video on reading in a dataset and writing out a dataset. Hope you found this useful, and happy coding!

For more infomation >> #Cartabianca - Brunetta commenta Sonny Olumati e lo Ius Soli - Duration: 8:12.
For more infomation >> Novos filmes e desenhos na Rede Brasil - Duration: 1:01. 
For more infomation >> Hazrat Muhammad s.a.w Aur Hazrat Fatima s.a In Hindu Book (Kalki) - Duration: 1:37.
For more infomation >> Bebê ruim dança na cama, aprende cores inglês com rima por Bebe Vida Real - Duration: 1:52.
For more infomation >> Sandy descarta fantasias sexuais com marido, mas afirma: 'Já usei roupa sexy' - Duration: 6:33.
For more infomation >> Rompe el silencio el guardia que descubrió al pistolero de las Vegas | Noticiero | Telemundo - Duration: 2:28.
For more infomation >> How I Make Money Online
For more infomation >> Saab 9-5 2.0T - Duration: 0:54. 














No comments:
Post a Comment