< Return to Video

Reading .csv files into RStudio

  • 0:02 - 0:04
    Hi. In this video I'm going
  • 0:04 - 0:08
    to show you how you can read in dot.
  • 0:08 - 0:10
    TXT files into R
  • 0:11 - 0:15
    so you can use that data to perform data
    analysis on.
  • 0:16 - 0:18
    So first thing you always need to do
  • 0:18 - 0:22
    is something we talked about in a previous
    video is set in your working directory.
  • 0:23 - 0:26
    So make sure that you have navigated
    to the folder
  • 0:26 - 0:30
    of where your data set
    is that you want to bring into R.
  • 0:31 - 0:33
    I have done so already.
  • 0:33 - 0:35
    I want to bring in this mri dot.
  • 0:35 - 0:37
    TXT file.
  • 0:37 - 0:39
    Okay.
  • 0:39 - 0:42
    The next thing you need to do
    is to read in a dot.
  • 0:42 - 0:45
    TXT file is to use the function read
  • 0:45 - 0:49
    dot table and inside the parentheses.
  • 0:49 - 0:52
    The first thing you'll put in there
  • 0:52 - 0:55
    is the name of the file
    that you want to bring in.
  • 0:55 - 0:59
    Now R is case sensitive,
    so you need to have it
  • 0:59 - 1:02
    be exactly how it's shown here.
  • 1:02 - 1:04
    Spaces underscores everything.
  • 1:04 - 1:09
    Plus you need to include the file
    extension okay.
  • 1:10 - 1:13
    And the next, important thing
  • 1:13 - 1:16
    that you need to also, specify
  • 1:16 - 1:19
    is whether or not,
  • 1:19 - 1:22
    you have a header to kind of help
    illustrate this.
  • 1:23 - 1:25
    Let's go ahead and read this data into R
  • 1:27 - 1:28
    okay.
  • 1:28 - 1:30
    What it will do is it'll show up down here
  • 1:30 - 1:34
    and it says, hey, I saw this data set
    and this is what it looks like.
  • 1:34 - 1:36
    So here it is.
  • 1:36 - 1:41
    Notice that it looks like my data has
    this first call.
  • 1:41 - 1:46
    Has this first at the top
    here has different column titles or like
  • 1:47 - 1:51
    headers, you know, to explain
    what each of these variables are.
  • 1:52 - 1:57
    But when I read it into R, notice
    that it counted
  • 1:57 - 2:01
    this first line as a piece of data
    as an observation.
  • 2:01 - 2:05
    When these are really actually
    just the column titles or the header.
  • 2:06 - 2:09
    So what we need to do
    is we need to specify
  • 2:10 - 2:12
    with a comma inside the parentheses
  • 2:12 - 2:15
    that are a header is equal to true,
  • 2:15 - 2:18
    meaning that we are telling R that,
  • 2:19 - 2:24
    hey, we have a header for our data
    like we have column titles.
  • 2:24 - 2:29
    So do not treat that very first row
    as a piece of data.
  • 2:29 - 2:31
    Treat it as the column titles.
  • 2:32 - 2:35
    Notice how are internally just called
  • 2:35 - 2:39
    all these v1 v2 like column one,
    column two, column three.
  • 2:40 - 2:43
    Watch what happens
    if I change this to header equals true.
  • 2:45 - 2:48
    Now you can see that R realized
  • 2:49 - 2:52
    or was told that this very first line
  • 2:52 - 2:55
    is the header or the column titles.
  • 2:55 - 3:00
    So now we can see that
    it is actually counting the right things
  • 3:00 - 3:04
    as observations of data
    and not counting these headers
  • 3:04 - 3:07
    as, as data.
  • 3:08 - 3:11
    If you do happen
    to not have headers on your data,
  • 3:11 - 3:14
    and you want R to do it for you,
  • 3:14 - 3:18
    you can always specify header equals false
    saying, hey, I don't have column titles.
  • 3:18 - 3:21
    Can you just call them v1, v2 for me.
  • 3:21 - 3:26
    But most of the time
    you will have column titles.
  • 3:26 - 3:29
    So don't forget to do this.
  • 3:29 - 3:31
    Header equals true.
  • 3:31 - 3:34
    Also notice that I'm doing it in all caps.
  • 3:35 - 3:35
    If I type it out
  • 3:35 - 3:39
    like this, R doesn't realize
    and it doesn't change color,
  • 3:39 - 3:44
    doesn't know that it's
    meaning the true value.
  • 3:44 - 3:46
    It's just thinking
    you're typing the word true.
  • 3:46 - 3:49
    So you need to make sure it's in all caps.
  • 3:49 - 3:53
    Or you can just say capital T
    or capital F.
  • 3:56 - 3:58
    But good practice
  • 3:58 - 4:01
    is to type out the full word.
  • 4:03 - 4:06
    Now you may have this data,
    but if you want to be able to do
  • 4:06 - 4:11
    any kind of, analysis
    with it, it's very important to,
  • 4:12 - 4:15
    make sure you, save it
  • 4:15 - 4:18
    and use what we call the assignment
  • 4:18 - 4:22
    operator to save it into your environment.
  • 4:22 - 4:24
    So that way you can use it later. Okay.
  • 4:24 - 4:27
    So instead of just running this
    read table,
  • 4:28 - 4:33
    command put in front of it the name of
    whatever you want to call your data.
  • 4:33 - 4:37
    So let's say
    I want to call this data MRI data.
  • 4:39 - 4:39
    And then
  • 4:39 - 4:44
    what you will put here is what we call
    the assignment operator in R,
  • 4:44 - 4:48
    it is a, less than sign with a dash.
  • 4:49 - 4:51
    And it's kind of like a little arrow.
  • 4:51 - 4:56
    So what that's doing is it's saying,
    okay, this part right here,
  • 4:56 - 5:00
    when we ran that notice,
    it just said boom, here's the data.
  • 5:00 - 5:03
    So what this
    now this whole line of code will do is it
  • 5:03 - 5:07
    will now take this data
    that we brought into R
  • 5:07 - 5:11
    and it will then store it into something
    like a little box
  • 5:11 - 5:14
    called MRI data.
  • 5:14 - 5:17
    So that way you can use it later.
  • 5:17 - 5:19
    Notice when I run this piece of code
  • 5:19 - 5:22
    what happens over here
    in your global environment.
  • 5:22 - 5:25
    It now saves your data as MRI data.
  • 5:25 - 5:29
    And it notices that there is 15
    observations of 11 variables,
  • 5:29 - 5:30
    which is true.
  • 5:30 - 5:34
    We have 15 observations
    and 11 different variables.
  • 5:34 - 5:37
    It does not show the data set here.
  • 5:37 - 5:40
    But you do see that has now been saved.
  • 5:40 - 5:43
    So now anytime
    you want to do anything with your data,
  • 5:43 - 5:47
    all you have to refer
    to it as you say, MRI data.
  • 5:48 - 5:49
    And when you
  • 5:49 - 5:53
    run it or do anything with it, it'll know
    that you're talking about this data set.
  • 5:54 - 5:55
    All right.
  • 5:55 - 5:58
    That is how you can read in
  • 5:58 - 6:03
    txt files into R
    and the importance of saving it.
  • 6:03 - 6:06
    And in your global environment.
  • 6:06 - 6:09
    And also don't forget to set your working
    directory at the very beginning.
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