Hi! I am Mike Marin and in this video
we will continue where we left off in the previous video; recall
we had imported the LungCapData into R . We discussed the use of the head
and tail command (or function) as well as the square brackets
for looking at subsets of the data. We also introduced the names command (names function)
we can check the variable names using the names command in R.
here we would like the names of the variables for LungCapData
now while we'll learn
R commands or functions for summarizing data later in the series of videos,
we will introduce the mean function for now for the sake of discussing what will follow:
suppose that we would like to calculate the mean Age in our sample
here we can try to ask R for the mean of the variable age.
We will see that we are returned an error
and R is telling us this object Age is not found
this is because R does not recognize a variable called
Age, as Age is stored in the object LungCapData
there are two options here: the first
is that we can use the dollar sign "$" to extract the variable
let's go ahead and demonstrate this; here
again we can ask R to calculate the mean and what we need to do
is going to this object LungCapData and using the dollar sign "$"
we can extract this variable Age. Just to reiterate that,
here we're asking R to go into this object LungCapData,
the dollar sign "$" specifies we would like to extract the variable
Age from this object and then we'll calculate the mean
for the variable Age. you'll notice that doing this
still does not put the variable Age into R's memory
we're trying to calculate the mean for Age. still gonna let us know this
object Age is not found. Or if we just ask R
to give us the Age for everyone in our dataset: this object is not found. if we'd
like to look at the Age
we need to let R know where Age can be found. look in this
object LungCapData and pull out of that object the variable Age
so the first option we had
is using the dollar sign "$" to extract variables. doing this
means every time we'd like to look at a variable we need to use the dollar sign
to extract it from the object
LungCapData. the second option that we have
is to attach the data. the pro of attaching data
is that we're able to call in variables by their name and we don't need to use
the dollar sign to extract them
the con is that they're put into R's workspace memory
and they can be overwritten more easily as well as they hang around in R's
memory until we erase them from R's memory. let's go ahead and look at
attaching the data. here we can use the command "attach"
and we'd like to attach the LungCapData into
R's memory. now if we try and calculate the mean
Age, all we need to do is let R know we would like to calculate the mean
for the variable Age. now R recognizes that there is a variable called
Age. again just to demonstrate that
if we ask R for the variable Age it will now
know that there is this variable Age in its memory and it will show all those to us
upon request. if you would like to "unattach" the data
we can use the "detach" command (detach function) "detach the LungCapData"
we can see having done this
if we ask for Age R no longer remembers this variable Age
okay I'm gonna go ahead and attach the data in R
and we're going to work with the data attached. it's worth mentioning here
that personally I prefer working with the data attached
as it means I don't need to use dollar signs to extract variables
all the time; others will tell you that is better to work with the data not
attached
really this is more of a personal preference
and really a matter of whether you prefer potatoes or potatoes!
we can check the type of a variable
or the class of a variable using the "class command" in R
the way that R treats a variable and produces
summaries for that variable will depend on the type a variable that
R thinks it is: that is a numeric variable
or a categorical variable. let's go ahead and first recall the names
of the variables in this object LungCapData
we can now use the "class" command to ask R what type of variable
each is. we can see the variable LungCap
is numeric, variable Age
it integer, variable Height
is numeric, variable Smoke
is a factor or categorical, variable
Gender is a factor and finally
variable Caesarean
is a factor; for factors
we can use the "levels command" to ask R what the different levels
or categories are for this factor or categorical variable
here we can see if we ask for the "levels" variable Smoke
it let us know they are "yes" and "no"
if we ask for the "levels" of the variable Gender
it let us know they are "female" and "male"
we will introduce the "summary command" in later videos
but for now let's ask R for a generic summary of the data
we can do this using the "summary command" for the LungCapData
we will see that R provide summaries it believes are appropriate for each of the
variables
we can see LungCap, Age and Height all of which were numeric
are summarized using "means", "median", "quartiles" and so on
we can see the variable Smoke, Gender, and Caesarean
which were factors or categorical are summarized using "frequencies"
that is the number of smokers and non-smokers or the number of females and
males
R was able to recognize Smoke, Gender and Caesarean
as factors as the data was entered using
characters or words; often in a dataset
categorical variables may be coded using numbers, for example
using 0 to represent a non-smoker and 1 to represent the smoker
or 0 to represent the male and 1 to represent the female
for the sake of looking at these let's go ahead and create
a 0-1 variable and save it in an object x
for the sake of demonstration let's pretend this is a variable
coding whether or not someone smokes: 0 for No 1 for Yes
we can see when we asked R what type of variable is
x, it tells us it's numeric and that's because R sees 0's and 1's as numbers
not as indicators for categories. if we ask for a summary
of this variable it's gonna report summaries appropriate for numeric
variables
we can convert x to a categorical variable or a factor
using the "as.factor" command in R
here we can let R know we would like x to be recorded
as a factor and in the parenthesis
we threw an x, again this is letting R know store in this object x
the object x recorded as a factor
we can now see when we ask R for the class of this variable x
it knows it's a factor, we ask for the summary
of this variable, it reports frequencies
there were 7 in category 0 and 3 in category 1
in the next video in this series we will discuss more about "subsetting data"
using the square brackets as well as introduce
the idea of logic statements and a few other random handy commands.
thanks for watching this video and make sure to check out my other instructional videos