R tutorial - Using Factors in R

preview_player
Показать описание
In this introduction to R course you will learn about the basics of R, as well as the most common data structures it uses to store data

If you have some background in statistics, you'll have heard about categorical variables. Unlike numerical variables, categorical variables can only take on a limited number of different values. Otherwise put, a categorical variable can only belong to a limited number of categories. As R is a statistical programming language, it's not a surprise that there exists a specific data structure for this: factors. If you store categorical data as factors, you can rest assured that all the statistical modelling techniques will handle such data correctly.

A good example of a categorical variable is a person's blood type: it can be A, B, AB or O. Suppose we have asked 8 people what their bloodtype is and recorded the information as a vector `blood`.

Now, for R it is not yet clear that you're dealing with categorical variables, or factors, here. To convert this vector to a factor, you can use the `factor()` function.

The printout looks somewhat different than the original one: there are no double quotes anymore and also the factor levels, corresponding to the different categories, are printed. R basically does two things when you call the factor function on a character vector: first of all, it scans through the vector to see the different categories that are in there. In this case, that's "A", "AB", "B" and "O". Notice here that R sorts the levels alphabetically. Next, it converts the character vector, blood in this example, to a vector of integer values. These integers correspond to a set of character values to use when the factor is displayed. Inspecting the structure reveals this:

We're dealing with a factor with 4 levels. The "A"'s are encoded as 1, because it's the first level, "AB" is encoded as 2, "B" as 3 and "O" as 4. Why this conversion? Well, it can be that your categories are very long character strings. Each time repeating this string per observation can take up a lot of memory. By using this simple encoding, much less space is necessary. Just remember that factors are actually integer vectors, where each integer corresponds to a category, or a level.

As I said before, R automatically infers the factor levels from the vector you pass it and orders them alphabetically. If you want a different order in the levels, you can specify the levels argument in the factor function.

If you compare the structures of `blood_factor` and `blood_factor2`, you'll see that the encoding is different now.

Next to changing the order of the levels, it is possible to manually specify the level names, instead of letting R choose them. Suppose that for clarity, you want to display the blood types as `BT_A`, `BT_AB`, `BT_B` and `BT_O`. To name the factor afterwards, you can use the `levels()` function. Similar to the names function to name vectors, you can pass a vector to levels blood_factor.

You can also specify the category names, or levels, by specifying the `labels` argument in `factor()`.

I admit it, it's a bit confusing. For both of these approaches, it's important to follow the same order as the order of the factor levels: first A, then AB, then B and then O. But this can be pretty dangerous: you might have mistakenly changed the order.

To solve this, you can use a combination of manually specifying the `levels` and the `labels` argument when creating a factor. With `levels`, you specify the order, just like before, while with the labels, you specify a new name for the categories:

In the world of categorical variables, there's also a difference between nominal categorical variables and ordinal categorical variables. The nominal categorical variables has no implied order. For example, you can't really say the the blood type "O" is greater or less than the blood type "A". "O" is not worth more than "A" in any sense I can think of. Trying such a comparison with factors will generate a warning, telling you that less than is not meaningful:

However, there are examples for which such a natural ordering does exist. Consider for example this tshirt vector. It has codes ranging from from small to large. Here, you could say that extra large indeed is greater than, say, a small, right?

Of course, R provides a way to impose this kind of order on a factor, thus making it an ordered factor. Inside the factor() function, you simply set the argument ordered to TRUE, and specify the levels in ascending order.

Can you so how these less then signs appear between the different factor levels? This compactly shows that we're dealing with an ordered factor now. If we now try to perform a comparison, this call for example, ..., evaluates to TRUE, without a warning message, because a medium was specified to be less than a large.
Рекомендации по теме
Комментарии
Автор

This is a super concise and well organized video on Factors.

buttman
Автор

What an absolutely fantastic video, high production quality, great clarity. Thank you very much!

coopernfsps
Автор

Very VERY helpful! I somehow did not get this the first time our teacher explained it. Thank you so much!

adiksaff
Автор

Excellent video. I am taking a class that does not go into much detail, and this video is the best supplement I could have asked for on this topic.

eldarion
Автор

I have taken the DataCamp website courses- the practice module is great, I dont have to juggle between windows on my laptop to exercise the skills taught by Filip. He is the best -for Python & R. I got so used to his way of teaching that it was difficult to see another person's method in the same DataCamp website.

taniag
Автор

Very concise & clear, absolutely loved it!!

AbhishekGupta-wxib
Автор

Great little video, I don't have the time for the long ones - thank you!

Iezident
Автор

Now I finally get factors. So clearly explained.

benparish
Автор

wow amazing👍 I cannot grasp ideas of factor function, But after seeing this video, it automatically solved. I'll Subscribe this.

june
Автор

This was incredibly helpful and well done. Thank you.

nwoodw
Автор

Thank you! The online class I was taking was getting factors after using just str() and I was not although we were using the same .csv file and then they just continued on so it was driving me insane. You filled in the blanks nice and quickly so at least now I know how to convert when necessary and understand more of what can be done with factors.

SaintMartini
Автор

Great and so clear - what about mentioning dplyr at the end

RobinBeaumont
Автор

This really helped me a lot, thank you.

Cynical_Engineer
Автор

I'm having an error in predicting. My code line is 'predict(rf, testData)'. Error is "New factor levels not present in the training data"

muhammadhamzahm
Автор

Do you have a video wherein you manipulate these factors with forcats package?

darrenmalibiran
Автор

Thanks a lot sir for uploading this video for helping students

viswajeettoshniwal
Автор

I thought there would be examples for us to practice

vascoambrosio
Автор

Thank you. This tutorial is awesome! Exactly what I needed!

aVersCloudSolution
Автор

The only thing I think would improve your otherwise very well done videos is if you added subtitles to everything he says. Or the option for it anyway.
The automatically youtube-generated subtitles have a lot of mistakes and are not sometimes not correct

moffig
Автор

hello sir, how can i convert a factor variable to be an simple vector...
like,
[1] 1 2 3 1 2 2 1 2 1 1 3
Levels: 1 2 3
Ans:
[1] 1 2 3 1 2 2 1 2 1 1 3

sangitaachary