Advertisement
English transcript
Hi! I am Mike Marin
and in this video we will introduce the use of "logic" commands
as well as few other random commands we will be working with the LungCapData
that was introduced earlier in the series of videos.I have already gone ahead
and imported the data into R and attached it. I'll now introduce the use of "logic" commands
how we can turn these into numeric indicators and how to bind columns together
these will be introduced working through a particular example
although their use can be applied to many different situations
we will restrict ourselves to only looking at the first five observations
so that we can see all the data on the screen and observe what's happening
we will begin by taking a look at the Age of the first five individuals
we can create a logic vector
or variable asking R whether or not Age is greater than 15
I will store this in an object called "temp" in here
we'd like an answer to the question: is Age greater than 15
we can see when looking at the first
5 observations of this object we are returned the answers: FALSE,
TRUE, TRUE, FALSE, and FALSE; now
let's take a look at the use of the "as.numeric" command
to have R return these as 0 & 1
indicators; 0 for value of FALSE
1 for TRUE. I will save these in an object "temp2"
and here we would like R to record
as a numeric variable an answer to the question
'is Age greater than 15' . again
taking a look at the first 5 observations we can see R is returning
0s where the answer is FALSE and 1 where the answer is TRUE
now let's go ahead and take a look
at the first 5 observations for all of our data
we can use
multiple logical statements within an R command
to have a logical vector answering multiple questions
for example we can create a vector indicating those who are female
and smoke. I'll save this in an object
called "FemSmoke". I would like an answer to the question
is gender equal to female
and is smoking
equal to yes and remember
we need to use double equal signs here to indicate the value of equality
here if we take a look at the first
five observations we can see
the only one for which a value TRUE is retuned
is the second individual and taking a look at the data
we can see this individual is a female and they do smoke
we can attach vectors
or matrices in a column-wise fashion using the "cbind" command
as well as attach them in a row-wise fashion using the "rbind" command
let's go ahead and attach this
FemSmoke variable to the entire dataset
using the "cbind" command. here I will save this
in an object called MoreData and would like to
bind together in a column-wise fashion the LungCapData
and would like to attach to these the variable FemSmoke
we can see if we take a look at the first
five rows of this data we now have
our original dataset and we've appended on
another column on the right side this variable FemSmoke
one final note on clearing the workspace: if you wish to remove
all objects from R's workspace you may do so using the drop-down menus within
RStudio
or you may use the following command: we would like
R to remove and the object we would like to remove
is the list of all objects
in the workspace memory. we can now see we've wiped
R's workspace clean.
thanks for watching this video and make sure to check out my other instructional videos
and in this video we will introduce the use of "logic" commands
as well as few other random commands we will be working with the LungCapData
that was introduced earlier in the series of videos.I have already gone ahead
and imported the data into R and attached it. I'll now introduce the use of "logic" commands
how we can turn these into numeric indicators and how to bind columns together
these will be introduced working through a particular example
although their use can be applied to many different situations
we will restrict ourselves to only looking at the first five observations
so that we can see all the data on the screen and observe what's happening
we will begin by taking a look at the Age of the first five individuals
we can create a logic vector
or variable asking R whether or not Age is greater than 15
I will store this in an object called "temp" in here
we'd like an answer to the question: is Age greater than 15
we can see when looking at the first
5 observations of this object we are returned the answers: FALSE,
TRUE, TRUE, FALSE, and FALSE; now
let's take a look at the use of the "as.numeric" command
to have R return these as 0 & 1
indicators; 0 for value of FALSE
1 for TRUE. I will save these in an object "temp2"
and here we would like R to record
as a numeric variable an answer to the question
'is Age greater than 15' . again
taking a look at the first 5 observations we can see R is returning
0s where the answer is FALSE and 1 where the answer is TRUE
now let's go ahead and take a look
at the first 5 observations for all of our data
we can use
multiple logical statements within an R command
to have a logical vector answering multiple questions
for example we can create a vector indicating those who are female
and smoke. I'll save this in an object
called "FemSmoke". I would like an answer to the question
is gender equal to female
and is smoking
equal to yes and remember
we need to use double equal signs here to indicate the value of equality
here if we take a look at the first
five observations we can see
the only one for which a value TRUE is retuned
is the second individual and taking a look at the data
we can see this individual is a female and they do smoke
we can attach vectors
or matrices in a column-wise fashion using the "cbind" command
as well as attach them in a row-wise fashion using the "rbind" command
let's go ahead and attach this
FemSmoke variable to the entire dataset
using the "cbind" command. here I will save this
in an object called MoreData and would like to
bind together in a column-wise fashion the LungCapData
and would like to attach to these the variable FemSmoke
we can see if we take a look at the first
five rows of this data we now have
our original dataset and we've appended on
another column on the right side this variable FemSmoke
one final note on clearing the workspace: if you wish to remove
all objects from R's workspace you may do so using the drop-down menus within
RStudio
or you may use the following command: we would like
R to remove and the object we would like to remove
is the list of all objects
in the workspace memory. we can now see we've wiped
R's workspace clean.
thanks for watching this video and make sure to check out my other instructional videos