-
Hi. In this video, I'm going to
show you how you can read
-
in, .csv files, data files into R
-
so that you can do your data analysis.
-
The first thing you need to do
is to set your working directory.
-
That is something that was described
and talked about in a previous video.
-
So navigate to the folder of wherever
-
the data set you want
to bring into R is located.
-
And I have already done so because
the data set I want to bring into
-
R is called MRI.csv.
-
Okay.
-
So if I want to bring in a CSV file-
-
Uh, CSV stands for Comma Separated Values.
-
Usually, it is opened up
in a program like Excel.
-
If I want to bring this data file into R,
-
I will use the read.csv function.
-
And inside
-
the parentheses I will put
the name of the data.
-
And R is case sensitive.
-
So you need to make sure
you spell it exactly how it is
-
saved as. Underscores, spaces, anything.
-
And then you always need
to include the file extension,
-
which is .csv.
-
Okay.
-
Let's go ahead and run this
and see what happens.
-
This is also really nice and looks good.
-
So it looks like it read
in our data just fine.
-
And it saw here,
-
"Oh look these are column titles."
-
So this is, if you saw the .txt video
that I did previously, this is something
-
that's different between the read.csv
function and the read.table function.
-
If your data-
-
notice like, what if I did
something like oh, sorry.
-
Wrong spot.
-
If I said this
-
command, I'll explain what
this means in a second.
-
Notice what R does.
-
It interprets
-
this very first line of code to be
-
an observation rather than-
though not this very first line.
-
And interprets the first line of the
data to be an observation of data,
-
when in fact it really is
-
the titles of your columns or the
names of all of your variables.
-
Read.csv is smart and
knows that most likely
-
your data is going to have
what we call a header.
-
But if your data happens
to not have a header-
-
So pretend that this first line
was actually data, but you wanted to
-
still be able to refer to the columns
as column one, two, three, four.
-
You could say header equals false,
meaning I don't have a header.
-
So please assign them
one, two, three, four, etc.
-
If you do have a header,
-
in our case we do
-
make sure to type out header equals true.
-
And notice what happens is that
-
R is able to notice that
-
the first line of your data
was the column titles.
-
And that is to not treat it
as an observation of data.
-
Good practice is to always
include this header argument,
-
and say header equals true
or false no matter what.
-
Notice also that I had to do in all caps.
-
If I try and just do capital T
-
or thinks I'm writing
just like the word true,
-
and it's not actually meaning internally,
like a logical value called true.
-
So I need to have it in all caps.
-
You can also do just the letter T
-
or just the letter F for true and false,
-
but it's better practice
to write out the entire word.
-
Okay, so this is nice that
-
you can bring in your CSV file into R,
-
but in order to do much of anything
with it, we need to kind of,
-
we need to save it into our R environment
-
so that we can do
further analysis with it.
-
So rather than just running
just this read.csv line,
-
in front of it, put, give this data,
-
set a name that you, that you can
call it, call it whatever you want.
-
I'm going to call it MRI data.
-
And then in between the name
and this command,
-
you're going to put in what we call
the assignment operator.
-
And it is a less than sign and a dash.
-
What this means is this little
piece of code right here.
-
Read.csv header equals true.
-
What that did is it brought
that data and said here it is.
-
But it didn't actually
do anything with it.
-
So what it's going to do is it's
going to take this data and store it.
-
And it kind of looks like it's like
pushing, the arrow is pushing into here.
-
It's going to take this data and store it
in a little box called MRI data.
-
If we run this line of code
-
notice what happens over here
in our global environment.
-
It now shows 'hey,
-
you have now stored a data
set here in R called MRI data.
-
There's 15 observations and
11 variables,' which is true.
-
We have 15 observations and 11 variables.
-
And now you should be able to do, use,
-
you can just refer to the data by whatever
name you called it which is MRI data.
-
And R will always know,
hey, that's the data set
-
I'm supposed to be using whenever,
-
you type MRI data.
-
So that is how you can read in
-
.csv files into R
-
and how you can then save
it into your environment
-
so that you can do further,
data analysis work with it.
-
Don't forget to set your working
directory at the very beginning,
-
and then remember that
R is case sensitive.
-
And to not forget the header argument.
-
And that should be
everything that you need.
-
Good luck and have fun in R.