< Return to Video

Reading .csv files into RStudio

  • 0:02 - 0:06
    Hi. In this video,
    I'm going to show you how you can read
  • 0:06 - 0:10
    in, CSV files, data files into R
  • 0:10 - 0:14
    so that you can do your, data analysis.
  • 0:14 - 0:18
    The first thing you need to do
    is to set your working directory.
  • 0:18 - 0:22
    That is something that was described
    and talked about in a previous video.
  • 0:23 - 0:25
    So navigate to the folder of wherever
  • 0:25 - 0:28
    the data set
    you want to bring into R is located.
  • 0:29 - 0:32
    And I have already done so
    because the data set I want to bring into
  • 0:32 - 0:35
    R is called MRI, dot, CSV.
  • 0:37 - 0:37
    Okay.
  • 0:37 - 0:40
    So if I want to bring in a CSV file,
  • 0:41 - 0:44
    CSV stands for comma separated values.
  • 0:44 - 0:47
    Usually it is opened up in a program
    like Excel.
  • 0:48 - 0:51
    If I want to bring this data file into R,
  • 0:51 - 0:55
    I will use the read dot csv function.
  • 0:56 - 0:57
    And inside
  • 0:57 - 1:00
    the parentheses
    I will put the name of the data.
  • 1:00 - 1:03
    And R is case sensitive.
  • 1:03 - 1:06
    So you need to make sure you spell it
    exactly how it is.
  • 1:06 - 1:10
    Saved as underscores spaces anything.
  • 1:11 - 1:14
    And then you always need to include
    the file extension
  • 1:15 - 1:17
    which is dot csv.
  • 1:17 - 1:17
    Okay.
  • 1:17 - 1:20
    Let's go ahead and run this
    and see what happens.
  • 1:22 - 1:25
    This is also really nice and looks good.
  • 1:25 - 1:28
    So it looks like it read in our data
    just fine.
  • 1:28 - 1:30
    And it saw here.
  • 1:30 - 1:33
    Oh look these are column titles.
  • 1:34 - 1:39
    So this is if you saw the first video
    that I did previously, this is something
  • 1:39 - 1:44
    that's different between the readcsv
    function and the read table function.
  • 1:47 - 1:50
    If your data
  • 1:50 - 1:54
    notice like what
    if I did something like oh, sorry.
  • 1:54 - 1:55
    Wrong spot.
  • 1:57 - 1:58
    If I said this
  • 1:58 - 2:01
    command, I'll explain what this means
    in a second.
  • 2:01 - 2:04
    Notice what R does.
  • 2:05 - 2:06
    It interprets
  • 2:06 - 2:09
    this very first line of code to be
  • 2:10 - 2:14
    an observation rather than, though
    not this very first line.
  • 2:14 - 2:15
    Okay.
  • 2:15 - 2:19
    And interprets the first line of the data
    to be an observation of data,
  • 2:19 - 2:21
    when in fact it really is
  • 2:21 - 2:24
    the titles of your columns
    or the names of all of your variables.
  • 2:25 - 2:30
    ReadCSV is smart
    and knows that most likely
  • 2:30 - 2:33
    your data is going to
    have what we call a header.
  • 2:34 - 2:37
    But if your data happens
    to not have a header.
  • 2:37 - 2:42
    So pretend that this first line
    was actually data, but you wanted to
  • 2:42 - 2:45
    still be able to refer to the columns
    as column one, two, three, four.
  • 2:46 - 2:49
    You could say header equals false, meaning
    I don't have a header.
  • 2:49 - 2:53
    So please
    assign them one, two, three, four, etc.
  • 2:54 - 2:56
    if you do have a header.
  • 2:56 - 2:58
    In our case we do
  • 2:59 - 3:02
    make sure to type out header equals true.
  • 3:02 - 3:04
    And notice what happens is that
  • 3:04 - 3:07
    R is able to notice that
  • 3:09 - 3:13
    the first line of your data
    was the column titles.
  • 3:14 - 3:19
    And that is to not treat it
    as an observation of data.
  • 3:20 - 3:24
    Good practice is to always
    include this header argument,
  • 3:25 - 3:28
    and say header equals true or false
    no matter what.
  • 3:29 - 3:32
    Notice also that I had to do in all caps.
  • 3:32 - 3:35
    If I try and just do capital T
  • 3:35 - 3:39
    or thinks I'm writing
    just like the word true,
  • 3:39 - 3:44
    and it's not actually meaning internally,
    like a logical value called true.
  • 3:45 - 3:47
    So I need to have it in all caps.
  • 3:47 - 3:50
    You can also do just the letter T
  • 3:50 - 3:53
    or just the letter F for true and false,
  • 3:53 - 3:57
    but it's better practice
    to write out the entire word.
  • 3:59 - 4:01
    Okay, so this is nice that
  • 4:01 - 4:05
    you can bring in your CSV file into R,
  • 4:05 - 4:11
    but in order to do much
    of anything with it, we need to kind of,
  • 4:11 - 4:14
    we need to save it into our environment
  • 4:14 - 4:17
    so that we can do further analysis
    with it.
  • 4:18 - 4:22
    So rather than just running
    just this readcsv line
  • 4:23 - 4:26
    in front of it, put, give this data,
  • 4:26 - 4:30
    set a name that you, that you can call it,
    call it whatever you want.
  • 4:31 - 4:34
    I'm going to call it MRI, AI data.
  • 4:35 - 4:38
    And then in between the name
    and this command,
  • 4:38 - 4:42
    you're going to put in what we call
    the assignment operator.
  • 4:42 - 4:45
    And it is a less than sign and a dash.
  • 4:47 - 4:51
    What this means
    is this little piece of code right here.
  • 4:51 - 4:53
    ReadCSV header equals true.
  • 4:53 - 4:57
    What that did is
    it brought that data and said here it is.
  • 4:57 - 5:00
    But it didn't
    actually do anything with it.
  • 5:00 - 5:04
    So what it's going to do is it's
    going to take this data and store it.
  • 5:04 - 5:08
    And it kind of looks like it's like
    pushing the arrow is pushing into here.
  • 5:08 - 5:13
    It's going to take this data and store it
    in a little box called MRI data.
  • 5:14 - 5:17
    If we run this line of code
  • 5:17 - 5:20
    notice what happens over here
    in our global environment.
  • 5:21 - 5:23
    It now shows hey,
  • 5:23 - 5:27
    you have now stored a data
    set here in R called MRI data.
  • 5:27 - 5:31
    There's 15 observations and 11 variables
    which is true.
  • 5:31 - 5:35
    We have 15 observations and 11 variables.
  • 5:37 - 5:40
    And now you should be able to do use.
  • 5:40 - 5:45
    You can just refer to the data by whatever
    name you called it which is MRI data.
  • 5:45 - 5:48
    And R will always know,
    hey, that's the data set
  • 5:48 - 5:51
    I'm supposed to be using whenever,
  • 5:52 - 5:55
    you type MRI data.
  • 5:55 - 5:58
    So that is how you can read in
  • 5:58 - 6:00
    dot CSV files into R
  • 6:00 - 6:03
    and how you can
    then save it into your environment
  • 6:03 - 6:06
    so that you can do further, data
    analysis, work with it.
  • 6:07 - 6:10
    Don't forget to set your working directory
    at the very beginning,
  • 6:10 - 6:13
    and then remember that
    R is case sensitive.
  • 6:13 - 6:15
    And to not forget the header argument.
  • 6:16 - 6:19
    And that
    should be everything that you need.
  • 6:19 - 6:21
    Good luck and have fun. And r.
Title:
Reading .csv files into RStudio
Video Language:
English
Duration:
06:23
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio
Utah_State_University edited English subtitles for Reading .csv files into RStudio

English subtitles

Revisions Compare revisions