Convert Categorical Variable to Numeric in R (Example) | Vector & Column | sapply & unclass Function

preview_player
Показать описание
R code of this video:

x <- factor(c("cat_a", "cat_b", "cat_a", # Create categorical vector
"cat_c", "cat_b", "cat_b"))
x # Print categorical vector

x_new <- unclass(x) # Convert categories tu numeric
x_new # Print updated vector

x2 = LETTERS[5:4],
x3 = "x",
stringsAsFactors = TRUE)

data_new <- sapply(data, unclass) # Convert categorical variables

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

The video has been so so helpful to me. Thanks for sharing.

naphtaliwanjala
Автор

Some people might miss that this will work specifically on Factor variables, not just any on Character variables. So for this to work in an actual dataset, they must first convert their Characters (Chr) variables into Factors.

ringgoldatienza
Автор

Hi this works perfectly for me thank you. Was just wondering what code do we need to add for the NA values to be ignored and not assigned a numerical value?

saracunha
Автор

Thanks for the help! Your channel is super helpful

andrewchapman
Автор

Can you help me My data is
Example
Weight BmiClass
60 Overweigth
54 Obese
55 Underweigth
45 Obese

So how you turn only 1 coloumn into numeric?

ReimundoEmeryBungkang
Автор

This isn’t working as explained in thé video. It’s converting all the numerical columns to non-numerical columns.

harishmaaashok