-
Hi. In this video I'm going
-
to show you how you can read in dot.
-
TXT files into R
-
so you can use that data to perform data
analysis on.
-
So first thing you always need to do
-
is something we talked about in a previous
video is set in your working directory.
-
So make sure that you have navigated
to the folder
-
of where your data set
is that you want to bring into R.
-
I have done so already.
-
I want to bring in this mri dot.
-
TXT file.
-
Okay.
-
The next thing you need to do
is to read in a dot.
-
TXT file is to use the function read
-
dot table and inside the parentheses.
-
The first thing you'll put in there
-
is the name of the file
that you want to bring in.
-
Now R is case sensitive,
so you need to have it
-
be exactly how it's shown here.
-
Spaces underscores everything.
-
Plus you need to include the file
extension okay.
-
And the next, important thing
-
that you need to also, specify
-
is whether or not,
-
you have a header to kind of help
illustrate this.
-
Let's go ahead and read this data into R
-
okay.
-
What it will do is it'll show up down here
-
and it says, hey, I saw this data set
and this is what it looks like.
-
So here it is.
-
Notice that it looks like my data has
this first call.
-
Has this first at the top
here has different column titles or like
-
headers, you know, to explain
what each of these variables are.
-
But when I read it into R, notice
that it counted
-
this first line as a piece of data
as an observation.
-
When these are really actually
just the column titles or the header.
-
So what we need to do
is we need to specify
-
with a comma inside the parentheses
-
that are a header is equal to true,
-
meaning that we are telling R that,
-
hey, we have a header for our data
like we have column titles.
-
So do not treat that very first row
as a piece of data.
-
Treat it as the column titles.
-
Notice how are internally just called
-
all these v1 v2 like column one,
column two, column three.
-
Watch what happens
if I change this to header equals true.
-
Now you can see that R realized
-
or was told that this very first line
-
is the header or the column titles.
-
So now we can see that
it is actually counting the right things
-
as observations of data
and not counting these headers
-
as, as data.
-
If you do happen
to not have headers on your data,
-
and you want R to do it for you,
-
you can always specify header equals false
saying, hey, I don't have column titles.
-
Can you just call them v1, v2 for me.
-
But most of the time
you will have column titles.
-
So don't forget to do this.
-
Header equals true.
-
Also notice that I'm doing it in all caps.
-
If I type it out
-
like this, R doesn't realize
and it doesn't change color,
-
doesn't know that it's
meaning the true value.
-
It's just thinking
you're typing the word true.
-
So you need to make sure it's in all caps.
-
Or you can just say capital T
or capital F.
-
But good practice
-
is to type out the full word.
-
Now you may have this data,
but if you want to be able to do
-
any kind of, analysis
with it, it's very important to,
-
make sure you, save it
-
and use what we call the assignment
-
operator to save it into your environment.
-
So that way you can use it later. Okay.
-
So instead of just running this
read table,
-
command put in front of it the name of
whatever you want to call your data.
-
So let's say
I want to call this data MRI data.
-
And then
-
what you will put here is what we call
the assignment operator in R,
-
it is a, less than sign with a dash.
-
And it's kind of like a little arrow.
-
So what that's doing is it's saying,
okay, this part right here,
-
when we ran that notice,
it just said boom, here's the data.
-
So what this
now this whole line of code will do is it
-
will now take this data
that we brought into R
-
and it will then store it into something
like a little box
-
called MRI data.
-
So that way you can use it later.
-
Notice when I run this piece of code
-
what happens over here
in your global environment.
-
It now saves your data as MRI data.
-
And it notices that there is 15
observations of 11 variables,
-
which is true.
-
We have 15 observations
and 11 different variables.
-
It does not show the data set here.
-
But you do see that has now been saved.
-
So now anytime
you want to do anything with your data,
-
all you have to refer
to it as you say, MRI data.
-
And when you
-
run it or do anything with it, it'll know
that you're talking about this data set.
-
All right.
-
That is how you can read in
-
txt files into R
and the importance of saving it.
-
And in your global environment.
-
And also don't forget to set your working
directory at the very beginning.