Presentation on demonstrating function "factor" in R
Ashu Guru
Author
03/07/2019
Added
491
Plays
Description
Presentation on demonstrating function "factor" in R
Searchable Transcript
Toggle between list and paragraph view.
- [00:00:00.320]We will now take a look at the function which is called factor.
- [00:00:05.440]In order to understand what factor helps us do,
- [00:00:09.013]let's first look at the file that contains our data.
- [00:00:12.960]So assume that we have a file which has first row as the column headers for a
- [00:00:22.434]table and there are 5 columns plot, entry name, block,
- [00:00:29.114]TSW and we have 9 rows of data.
- [00:00:33.520]Another thing to notice is that even though that the scientist has used
- [00:00:40.402]numbers to represent plots, they are actually categorical variables
- [00:00:46.903]or what should be treated as factors.
- [00:00:52.160]Similarly, the entries are categorical variables.
- [00:00:58.920]That means they actually represent levels of a factor rather than a numeric value,
- [00:01:05.417]even though we are using a numeric ID to represent them.
- [00:01:10.200]The name give us a indication that it is actually a categorical variable because
- [00:01:15.480]two of the values in that column are string literals.
- [00:01:20.720]And again, consider that block for the experiment is
- [00:01:25.270]actually another categorical variable, but the scientist chose to use an ID or a
- [00:01:32.225]number to represent it.
- [00:01:35.440]Now when we read this table in R, by default R we'll be able to understand
- [00:01:41.440]the name is actually a categorical variable because it finds that few of the
- [00:01:47.600]values in that column are string literal, so it understands.
- [00:01:52.480]I'm going to treat this as a factor variable with different levels.
- [00:01:58.080]However, for our to implicitly treat plot entry
- [00:02:02.620]and block is not possible.
- [00:02:05.360]So somehow we will have to force our to think to treat plot name and plot entry
- [00:02:11.013]and block also as categorical variables.
- [00:02:13.840]And this is done using factor function.
- [00:02:17.040]So let's take a look.
- [00:02:19.480]So as a first step I'm going to change the working directory of the of R to the
- [00:02:25.141]folder where I have my text file or my CSV file with the data.
- [00:02:30.280]So I'm going to run the set WD command and now I will load the file which is
- [00:02:39.496]testdata. CSV in a variable called field data table.
- [00:02:51.600]Let's see the structure of this new variable that we created.
- [00:02:56.360]As I mentioned earlier that R was able to identify name as a factor variable.
- [00:03:02.960]However, plot is still integer, entry is still integer and block is still
- [00:03:09.962]integer.
- [00:03:10.720]However, in our experiment the scientist has just
- [00:03:14.454]used numbers to represent these categorical values but the variable
- [00:03:19.532]itself is actually a factor or a categorical variable.
- [00:03:23.640]So now we are going to use a function called factor.
- [00:03:27.160]And what it basically says is that in this table variable,
- [00:03:35.464]the column value plot treat this as factor where the values come from the
- [00:03:45.881]field same tables column.
- [00:03:51.520]And I'm going to run the same command for entry and block as well.
- [00:03:58.360]And finally I'm going to print the structure of the variable field data
- [00:04:03.880]table again.
- [00:04:04.800]So now if we compare the previous print out to the new print out,
- [00:04:10.271]we can see that now R understands that plot must be treated as a factor,
- [00:04:16.323]entry must be treated as a factor, block must be treated as factor,
- [00:04:21.961]which is unlike the initial readout which said plot and entry was integer and block
- [00:04:28.925]was integer.
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/10750?format=iframe&autoplay=0" title="Video Player: Presentation on demonstrating function "factor" in R" allowfullscreen ></iframe> </div>
Comments
0 Comments