Thursday, December 14, 2017

How do you use factor/s in R Programming?

Factors are variables to represent categories. Categories are discrete and not continuous. In statistics there is a need for such a variable.

Let us say, I roll the dice a dozen times and come up with values for each roll as shown:
2,4,5,3,2,6,10,5, 9,6,4,5

I can gather these in a data as shown in R with "dat":
data
---------------------------------






---------------------------------
Now I use 'factor' as used in R:
facData=factor(data)
--------------------

-----------------------
factor just took one unique item from the list presented to it to display in Levels. There were two 4's, three 5's and two 6's. It categorized them.

While the original data was numeric they could be also character data in "datac" below:


-------------------------------
We know the unique ones in the above data. How are we going to find how many of each are there?
For this we can use the table() in R as shown below:
------------------------------------

You can check for yourself, that it picked the correct value for each category.

No comments: