Hey guys, in this video I wanted to do a quick and really simple
introduction to R Studio, specifically talking about how to import data. That's
pretty much all I'm gonna show in this video and again specifically I'm talking
about how to import it using the GUI and I'll just give you some information
about importing data into R Studio in general as I go along.
This is kind of like a video geared towards people whose first time it is
using R Studio. All right so there's a GUI if you go to Tools > Import Dataset
and then if you have a file saved to your computer, you can click "From Local
File" and then it'll show your documents-- ignore all the random folders I have--and
then I'm just gonna pick a CSV file. One of the first things I wanted to say
about this GUI was or R studio in general is there are a couple different
file extensions that you can import into R Studio. If you have one that's like a
weird extension in particular you can probably look up online if R
can import it. This particular GUI, you'll see after I click import, it produces R
code called read.CSV. So read.CSV is a command that can read obviously dot CSV
files, so that's comma separated value files. The one that I just imported, "Reg
Test", is a comma separated value file so as you can see here the commas separate
or act as separators in the file and then it produces commas when you import
it's read.CSV the command and this GUI can import CSV files and also dot
txt files, so dot txt. Now if you're importing files from an excel sheet for
example, you can open it in excel and save your file as dot CSV and then run
this command, that's fine. If you try to import using the GUI a dot
it's like an excel extension so dot xlsx or whatever the newer extension is this
GUI will not work. You can still import it into R there's a code read.xlsx
but you're gonna have to write the code out you can't use the GUI. Okay so anyway
going back to this file, so as you can see here it has a little preview window.
So you just want to check that it is going to import it correctly. It looks
like it is. This name--the name that they
automatically fill in is the one that--the way you saved the file. You can
change that if you want to. This is fine. The heading, yes or no, that just asks you
if you have head--if the original data file has headers. In a lot of
statistics files, the first row is going to not be actual data it's going to be
information about the columns and the and the data that follows
so in this case I do have headers. I'm going to leave that as yes. If you don't
have these headers in your data file, obviously you click no. This is fine. As
you can see this is comma, so that's fine. Decimal, that's just asking if you have
periods for decimals. I live in the United States so that's how we do it, in
other countries they sometimes use commas. That's fine. I don't have any
quotes but if you do, you can select double quote or single quote. I don't
have any comments either in my data. This is also important, so if you have a data
or data file with missing values, you don't want to leave those empty and you
don't want to put those as zeroes. You want to put those as NAs if possible
and so the NA strings that's gonna be left alone. Actually this file doesn't
have any empty data either, so we're good to go. We can click import and you'll see
that this runs that code--these two lines of code in the console. I'm
gonna just copy it and paste it here. The code that was run, like I said was CS--
read.CSV--and it assigns it using this assignment operator to a variable that
was created called RegTest. Again it just gets that name from the file name
that I imported and it picks up the file from the local computer. And then the
second line of code that it runs automatically is view in parentheses Reg
Test and that just produces this data frame. So you've imported the data
successfully and you can see it's in your environment window as well. One more
thing I would say is that for R, you always have to attach your data frame or
your data after you import it. For example, if you try to run a regression
if you look at my data I have, for example, left thalamus proper and say I
want to regress that on age. Okay so left thalamus proper regressing on age. If you
highlight a line and click ctrl + enter it'll run in the console. I'm just
going to do that. As you can see, we have an error. R studio won't recognize
the data you just imported because you always have to also add the code
attach and then the name of the data frame. I'm going to run that. All right
the data is attached. Now if I try to run this same line it works, as you see.
You can't actually access the data until you attach it, which I think
they should have included in the GUI but they didn't. Anyway that's all I'm
gonna put in this video. I think if anyone has any--if you want me
to make a video about anything in R Studio in particular maybe I can make a
series about R studio. So leave me a comment below if you want to see
anything. Okay thank you, bye