Presentation on demonstrating function "within" in R
Ashu Guru
Author
03/13/2019
Added
598
Plays
Description
Presentation on demonstrating function "within" in R , by Ashu Guru, University of Nebraska Raikes School (4.05 mins)
Searchable Transcript
Toggle between list and paragraph view.
- [00:00:00.640]So many times we have to add variables to the data that we have and the goal is to
- [00:00:07.435]add some sort of a variables that we can use for helping us let's say group or
- [00:00:13.903]identify.
- [00:00:14.640]And in this case, we are adding variables mostly to make
- [00:00:18.979]groups of data within this old data.
- [00:00:21.800]So if we look at the example file that we have been working with,
- [00:00:26.902]we know that all the entry types that are new have their entry ID which is less
- [00:00:33.087]than or equal to 50 or in other words the checks have the entry ID which is greater
- [00:00:39.581]than 50.
- [00:00:40.200]So what we want to do is we want to add another variable to the data frame that
- [00:00:46.707]reads this file and we will call it as new that will say 0 if we are reading an
- [00:00:53.215]entry that is check and one if we are reading an entry that is the new entry.
- [00:00:59.680]Let's see how we can do this in R So to do this in our we are going to use what
- [00:01:04.981]is called as a within function or a within statement.
- [00:01:08.920]And this statement allows us to iterate or loop through or walk through the
- [00:01:14.051]different elements of a variable.
- [00:01:16.280]In this case we have a table or a data frame.
- [00:01:19.120]So the within variable will walk through each row and then with that row we can
- [00:01:24.112]perform some action.
- [00:01:25.360]So let's look at this here.
- [00:01:28.000]Here's a within statement.
- [00:01:29.200]So I'm saying within data frame or the table if my condition is that if the
- [00:01:35.832]entry ID is greater than 50 then return the value 0 otherwise one.
- [00:01:41.920]So this is a short form of if else statement basically saying that if this
- [00:01:46.470]condition is true then return the first value otherwise the second value.
- [00:01:51.120]We are now assigning whatever is returned by this if else statement to a new
- [00:01:56.454]variable new which will be a column to the actual dataframe.
- [00:02:00.720]So let's take a look.
- [00:02:02.240]I am going to define a working directory where my file with the data is located.
- [00:02:08.520]So I set the working directory with set WD command.
- [00:02:12.680]This instruction tells that I'm creating a file name variable called input file.
- [00:02:19.240]I read the file in a variable called input data and then I am going to run the
- [00:02:26.051]structure command which tells me that this input data is a variable of type
- [00:02:32.605]data frame which has 68 observations and five variables.
- [00:02:37.760]So we have plot entry name block TSW and I am going to run the head command and it
- [00:02:46.121]shows me the top 6 lines.
- [00:02:49.560]We see that coplot, entry name block and TSW has the
- [00:02:53.739]following values.
- [00:02:55.920]The next three lines that I have are the within instruction which will go and add
- [00:03:04.022]a value of 0 when R finds the corresponding column.
- [00:03:09.160]Value for the entry is greater than 50 and value one when the corresponding
- [00:03:17.423]value is less than or equal to 50.
- [00:03:21.360]Let's run these instructions and now when I look at the structure,
- [00:03:25.922]it shows that there's a new column in our data frame.
- [00:03:31.320]And I'm going to run a head command and it shows me that wherever R found that
- [00:03:37.685]the entry was less than 50, it made the variable value as one,
- [00:03:42.761]and if it was greater than 50, it made the variable value as zero.
- [00:03:48.400]Now we can use this new column for grouping everything that is a new entry
- [00:04:00.250]and ones that are checks.
The screen size you are trying to search captions on is too small!
You can always jump over to MediaHub and check it out there.
Log in to post comments
Embed
Copy the following code into your page
HTML
<div style="padding-top: 56.25%; overflow: hidden; position:relative; -webkit-box-flex: 1; flex-grow: 1;"> <iframe style="bottom: 0; left: 0; position: absolute; right: 0; top: 0; border: 0; height: 100%; width: 100%;" src="https://mediahub.unl.edu/media/10792?format=iframe&autoplay=0" title="Video Player: Presentation on demonstrating function "within" in R" allowfullscreen ></iframe> </div>
Comments
0 Comments