Advertisement
English transcript
Hi! I am Mike Marin and in this video
we'll talk about getting started working with data in R
We will read in a dataset called "LungCapData"
this is a tab-delimited text file, we can use the "read.table" command
to import data into R; there are many other ways of getting data into R
we're just gonna discuss a few here; you can access the help menu
by typing "help" and in brackets the name of the command you'd like help for
or by placing a question mark
in front of the command; we will go ahead
and read the data into R and save it in an object called "Data1"
here will use the "read.table" command
the first thing to specify is the file
we must specify the path to the file
in quotations. I've already found the path to my file and copied it
so I am just gonna go ahead and paste it in here. the next argument
is the "header" argument; here we can set this to TRUE
letting R know that the first row of our data is headers
or the variable names; the last argument we should worry about
is the "sep" argument: this lets R know how
our observations are separated. Our file is tab-delimited
so we'll use a "slash t" to let R know this is tab-delimited data
if instead our data were comma-separated
we can either insert a comma here or we can use the "read.csv" command
if observations are separated by white space
we can just leave this blank. I'm gonna go ahead and put the "\t" back in here
as our data is tab-delimited. once we enter this
we can see in the workspace we now have this object "data1"
which contains 725 observations
on six variables. a second option
which is a little bit easier and doesn't involve specifying the path to the file
is to use the "file.choose" command within the "read.table" command
let's go ahead and read the data into R again and we will save it
in an object called "Data2". here we're going again
use the "read.table" command, specifying the file
we can type "file.choose" and end that with open and close parenthesis
next we can set the "header" argument equal to TRUE
letting R know the first row of our data
are headers or variable names and finally
we can set the "sep" argument equal to "\t"
again letting R know this is a tab-delimited file
now R give us this drop-down menu
where we can select the file. I have this file saved on my desktop
in a folder called R Videos and the file saved right here
LungCapData.txt ;
we can now see in R's workspace this object "Data2"
again 725 observations
on 6 variables; we will talk about one more
even simpler option to getting data into R
and this can only be done if you're working with R studio
not just R itself; here we can use the
"Import Dataset" menu, when we select this
we see we have two options to import data from a text file
or from the Web,we will select import data from a text file or from the web
again we get the drop down menu, the data that is stored
on my desktop in this R Videos file
save this LungCapData; once we select this
we will see R gives us one more drop down menu
here we can specify the name of the object we would like to save the data in
previously we say that as "Data1" or "Data2"
here Ill save it as "LungCapData". the next options is to specify
whether or not there is a header use here was like yes
letting R know the first row of the data are headers or variable names
the next option is to let R know how the data is separated
Ours is just tab-delimited, we can see other options of
semicolon a comma or white space
the third option is to let R know
which character will specify a decimal point
here we use periods for decimal points in Europe
it's common to have a comma representing a decimal point
the final option is to let R know what to do
with categorical variables or character strings
here will have R place double quotes around character strings
instead of a single quote or no quotations
now that we specified all the options we can go ahead and import the data
we can now see this third object in R
"LungCapData" again 725 observations on six variables
we can also see up here the data showing in
R Studio's data view
scroll through the entire set of data if we like
it's worth noting here that reading data into R
can be painful at first so go ahead and use what you know
I personally find is easiest to work with Excel
and save the data in a format I'm more familiar with there
for example I usually prefer to get the data
organized in Excel and then save it as a tab-delimited text file
we now have the data save in three separate objects in R
let's remove the Data1 and Data2 objects
using the "rm command" that we learned earlier
here we'd like to remove the object "Data1" from the workspace
as well as remove the object "Data2" from the workspace
we did this simply for the sake of cleanliness and being a bit more
organized
now we'd like to make sure we've got the data into R Studio
as it should be. we can take a look at the data viewer as we just did
or we can use some commands in R to check this
the first command we might want to use is the "dim command"
this will let us know the dimensions of the data in R
that is the number of rows and the number of columns. We would like to know the
dimensions
for this object "LungCapData"
we can see it has 725 rows and six columns
to view a portion of the data we can use the"head command"
asking R to see the first six rows of this object LungCapData"
or we can use the "tail command" the see the last six rows
of this object "LungCapData"
we will work with this "LungCapData" through most of the videos in this
series
we can see our outcome variable is "LungCap" a measure of
lung capacity and we have five explanatory variables
we would like to relate lung capacity to
the age of the child, the height of the child, whether or not they identify as a
smoker
their gender and whether or not they were born via Caesarean
another way we can check to make sure the data was read into R
as it should be is to use the square brackets to subset the data
as we learned in the last video. we can look at the "LungCapData"
in here ask for rows 5, 6, 7, 8, 9
and leave this blank specifying all columns
we've also learned in the last video
that we can use the colon to create a sequence
rows 5 up to 9 and again leaving this blank selecting all columns
then again just remind ourselves that one more thing we learned in previous
videos
you can look at "LungCapData" creating a sequence
running from 4 up the 722
and throwing a negative sign (-) in front of this is going to give us
all rows except rows 4 up to 722
and again we will leave this blank specifying we'd like all columns
we can check the variable names using the "names command" in R
here we would like the names of variables for the "LungCapData"
in the next video in this series we will continue on from here
and discuss checking variables in R
Thanks for watching this video and make sure to check out my other instructional videos
we'll talk about getting started working with data in R
We will read in a dataset called "LungCapData"
this is a tab-delimited text file, we can use the "read.table" command
to import data into R; there are many other ways of getting data into R
we're just gonna discuss a few here; you can access the help menu
by typing "help" and in brackets the name of the command you'd like help for
or by placing a question mark
in front of the command; we will go ahead
and read the data into R and save it in an object called "Data1"
here will use the "read.table" command
the first thing to specify is the file
we must specify the path to the file
in quotations. I've already found the path to my file and copied it
so I am just gonna go ahead and paste it in here. the next argument
is the "header" argument; here we can set this to TRUE
letting R know that the first row of our data is headers
or the variable names; the last argument we should worry about
is the "sep" argument: this lets R know how
our observations are separated. Our file is tab-delimited
so we'll use a "slash t" to let R know this is tab-delimited data
if instead our data were comma-separated
we can either insert a comma here or we can use the "read.csv" command
if observations are separated by white space
we can just leave this blank. I'm gonna go ahead and put the "\t" back in here
as our data is tab-delimited. once we enter this
we can see in the workspace we now have this object "data1"
which contains 725 observations
on six variables. a second option
which is a little bit easier and doesn't involve specifying the path to the file
is to use the "file.choose" command within the "read.table" command
let's go ahead and read the data into R again and we will save it
in an object called "Data2". here we're going again
use the "read.table" command, specifying the file
we can type "file.choose" and end that with open and close parenthesis
next we can set the "header" argument equal to TRUE
letting R know the first row of our data
are headers or variable names and finally
we can set the "sep" argument equal to "\t"
again letting R know this is a tab-delimited file
now R give us this drop-down menu
where we can select the file. I have this file saved on my desktop
in a folder called R Videos and the file saved right here
LungCapData.txt ;
we can now see in R's workspace this object "Data2"
again 725 observations
on 6 variables; we will talk about one more
even simpler option to getting data into R
and this can only be done if you're working with R studio
not just R itself; here we can use the
"Import Dataset" menu, when we select this
we see we have two options to import data from a text file
or from the Web,we will select import data from a text file or from the web
again we get the drop down menu, the data that is stored
on my desktop in this R Videos file
save this LungCapData; once we select this
we will see R gives us one more drop down menu
here we can specify the name of the object we would like to save the data in
previously we say that as "Data1" or "Data2"
here Ill save it as "LungCapData". the next options is to specify
whether or not there is a header use here was like yes
letting R know the first row of the data are headers or variable names
the next option is to let R know how the data is separated
Ours is just tab-delimited, we can see other options of
semicolon a comma or white space
the third option is to let R know
which character will specify a decimal point
here we use periods for decimal points in Europe
it's common to have a comma representing a decimal point
the final option is to let R know what to do
with categorical variables or character strings
here will have R place double quotes around character strings
instead of a single quote or no quotations
now that we specified all the options we can go ahead and import the data
we can now see this third object in R
"LungCapData" again 725 observations on six variables
we can also see up here the data showing in
R Studio's data view
scroll through the entire set of data if we like
it's worth noting here that reading data into R
can be painful at first so go ahead and use what you know
I personally find is easiest to work with Excel
and save the data in a format I'm more familiar with there
for example I usually prefer to get the data
organized in Excel and then save it as a tab-delimited text file
we now have the data save in three separate objects in R
let's remove the Data1 and Data2 objects
using the "rm command" that we learned earlier
here we'd like to remove the object "Data1" from the workspace
as well as remove the object "Data2" from the workspace
we did this simply for the sake of cleanliness and being a bit more
organized
now we'd like to make sure we've got the data into R Studio
as it should be. we can take a look at the data viewer as we just did
or we can use some commands in R to check this
the first command we might want to use is the "dim command"
this will let us know the dimensions of the data in R
that is the number of rows and the number of columns. We would like to know the
dimensions
for this object "LungCapData"
we can see it has 725 rows and six columns
to view a portion of the data we can use the"head command"
asking R to see the first six rows of this object LungCapData"
or we can use the "tail command" the see the last six rows
of this object "LungCapData"
we will work with this "LungCapData" through most of the videos in this
series
we can see our outcome variable is "LungCap" a measure of
lung capacity and we have five explanatory variables
we would like to relate lung capacity to
the age of the child, the height of the child, whether or not they identify as a
smoker
their gender and whether or not they were born via Caesarean
another way we can check to make sure the data was read into R
as it should be is to use the square brackets to subset the data
as we learned in the last video. we can look at the "LungCapData"
in here ask for rows 5, 6, 7, 8, 9
and leave this blank specifying all columns
we've also learned in the last video
that we can use the colon to create a sequence
rows 5 up to 9 and again leaving this blank selecting all columns
then again just remind ourselves that one more thing we learned in previous
videos
you can look at "LungCapData" creating a sequence
running from 4 up the 722
and throwing a negative sign (-) in front of this is going to give us
all rows except rows 4 up to 722
and again we will leave this blank specifying we'd like all columns
we can check the variable names using the "names command" in R
here we would like the names of variables for the "LungCapData"
in the next video in this series we will continue on from here
and discuss checking variables in R
Thanks for watching this video and make sure to check out my other instructional videos