convert categorical to numeric (2024)

Peter Perkins on 23 Mar 2018

  • Link

    Direct link to this answer

  • Link

    Direct link to this answer

Open in MATLAB Online

Calling categorical is a data conversion, so

 c = categorical([12 12 13])

completely throws away the numeric values. In general, there is no way to get them back unless you have saved them, any more than you can get back the original values from int8([1.1 2.2 3.3]). Calling categorical is a data conversion.

That being said, you can certainly save the unique numeric values, and then index into those using the categorical array:

 n = uniqueNumericValues(c)

You can also call double on a categorical, but what you will get back are the category numbers, not the original numeric values.

But here's the question: if you need to convert back to the original numbers, and you are not using meaningful category names when converting from those numbers, why use categorical to begin with? There may be things you haven't mentioned.


Show 2 older commentsHide 2 older comments

Ian Blake on 4 Jun 2019

Direct link to this comment

  • Link

    Direct link to this comment

I have the same problem, and the help file.... does not help at all

My data is categorical because the importdata chose that for it, I can force but then if I import new data and don't force it to numerical, my processing will stop working. I'm running a script so I can put a conversion there -> automate not rely on human memory!

In particular I have 160,000 lines of data in a table, one of 46 fields is an odo reading. This has converted to categorical, with 16983 categories - so might be more efficient, fair enough. But now I want to plot data against odo, so I need numerical. example subset:

>> catdata

ans = 1×8 categorical array

37241 37364 37099 4264 6339 38209 38070 16777215

So the original numbers are NOT lost, but are coded in the categories:

>> catcats=categories(catdata);

>> length(catcats)

ans = 16983

As noted above, double () gives the index not the value

>> double(catdata)

ans =

10880 10902 10858 11593 13789 11022 11004 4659

>> catcats(4659)

ans = 1×1 cell array


But cell2mat gives you a string not a number:

>> cell2mat(catcats(4659))

ans = '16777215'

So you then need to convert again using str2num (why no cell2num? There is a num2cell):

>> str2num(cell2mat(catcats(4659)))

ans = 16777215

So this works for one item, but when I use the 8 element data with the resulting strings being different length, it fails

>> catcats(double(catdata))

ans = 8×1 cell array

{'37241' }

{'37364' }

{'37099' }

{'4264' }

{'6339' }

{'38209' }

{'38070' }


>> cell2mat(catcats(double(catdata)))

Error using cat

Dimensions of matrices being concatenated are not consistent.

Error in cell2mat (line 83)

m{n} = cat(1,c{:,n});

This seems like way more difficult than it should be.

Peter Perkins on 5 Jun 2019

Direct link to this comment

  • Link

    Direct link to this comment

The fundamental problem is that your numeric data are being read in as categorical. I don't have your file, so I can't tell why that is, but I recommend you use detectimportoptions, and set the type, and use that in calls to readtable to read in all of your other data.

Ian Blake on 10 Jun 2019

Direct link to this comment

  • Link

    Direct link to this comment

Edited: Ian Blake on 10 Jun 2019

Open in MATLAB Online


I've come to the conclusion that would have been easiest, although I've developed an effective though crude workaround.

vfdbdata.DD01km is my categorical data array (from a table of data)

odocats = categories (vfdbdata.DD01km);

odoval = zeros (1, length (odocats) ); % preallocate space

for kk=1:length(odocats),



So this is run before the main processing, the numeric data can then be extracted as required by

odotemp = double ( vfdbdata.DD01km(vidx) ) ;

odotemp = odoval (max (1, odotemp) ) ;

The max ensures that 'undefined' values are processed without throwing an error (they give a NaN after translation to double, which causes a subscript error), I also have some code to process specific values that can occur (hence the use of a temporary variable).

Matthew Anderson on 13 Apr 2020

Direct link to this comment

  • Link

    Direct link to this comment

Open in MATLAB Online

a = categorical(["2" "3" "3"])

double(a) % returns [1 2 2] - maybe desired for some reason

double(string(a)) % returns [2 3 3] - maybe desired for some reason

categorical(double(string(a)) % returns the same thing as a

Sign in to comment.

convert categorical to numeric (2024)


How to convert categorical data into numeric? ›

You can either create a distinct category for missing values, assign them unique numerical values, or use imputation techniques. Imputation involves replacing missing values with the mode for nominal data or the median for ordinal data. For more advanced approaches, consider using machine learning algorithms.

How to convert categorical data into numeric in Excel? ›

How to Convert Categorical Data to Numeric in Excel
  1. Step 1: Enter the Data. First, enter the data values into Excel: ...
  2. Step 2: Use the IFS Function to Convert Categorical Values to Numeric Values. ...
  3. Step 3: Drag the Formula Down to All Cells.
May 12, 2022

Which method is used to convert categorical to numeric attributes? ›

Target encoding is a technique used in machine learning and data preprocessing to transform categorical variables into numerical values.

What is the process of transforming categorical variables into numerical values called? ›

Categorical encoding is the process of transforming a categorical column into one (or more) numeric column(s). This is necessary because computers are more at ease working with numbers than with strings.

How do you convert character to numeric data type? ›

Using as.

numeric() function in R is your go-to for converting character data into numeric form. This transformation is crucial for subsequent data analysis tasks that require numerical input.

Can a categorical variable be numeric? ›

Although categorical data is qualitative, it can also be calculated in numerical values. However, these possible values don't have quantitative qualities—meaning you can't calculate anything from them. Categorical data may also be classified as binary and nonbinary depending on its nature.

How do I change a character variable to numeric in Excel? ›

Use Paste Special and Multiply

Select the cells that have numbers stored as text. On the Home tab, select Paste > Paste Special. Select Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.

How do I change data type to numeric in Excel? ›

To convert cells that are formatted as text to numbers, follow these steps:
  1. Select the cell that is formatted as text that you want to convert to a number. ...
  2. On the Error Checking Options button, click the down arrow. ...
  3. Click Convert to Number.

How is categorical data encode to numerical data? ›

Ordinal Encoding is a technique used to convert the categorical data into a numerical format. As the name suggests, it's particularly suited to ordinal categorical variables, where the categories have an inherent order or hierarchy.

How to deal with categorical data? ›

  1. Step 1: Drop columns with categorical data. You'll get started with the most straightforward approach. ...
  2. Step 2: Label encoding. Before jumping into label encoding, we'll investigate the dataset. ...
  3. Step 3: Investigating cardinality. ...
  4. Step 4: One-hot encoding.

What is the difference between categorical and numeric attributes? ›

A categorical variable is a variable with a set number of groups (gender, colors of the rainbow, brands of cereal), while a numeric variable is generally something that can be measured (height, weight, miles per hour).

Can categorical data be transformed? ›

Before categorical data can be utilized as input to a machine learning model, it must first be transformed into numerical data. This process of converting categorical data into numeric representation is known as encoding. There are two types of categorical data: nominal and ordinal.

How to convert categorical variable to numerical variable in R? ›

You can use the factor() function to convert the categorical variables to factors and then use the as. numeric() function to convert the factors to numerical variables.

How to change categorical data to numerical data in DataFrame? ›

In Pandas, there are several ways to convert categorical data to numerical data, including the following:
  1. Method 1: Using the Attribute. The easiest way to convert categorical data to numerical data in Pandas is to use the cat. ...
  2. Method 2: Using the replace() Method. ...
  3. Method 3: Using the LabelEncoder Class.
Jun 19, 2023

How to change a categorical variable to numeric in R? ›

Target Encoding: Target Encoding is a process of converting categorical variables into numerical variables by replacing each category with the mean of the target variable for that category. You can use the targetMean() function from the cattonum package to create target encoding.

How to change categorical data to numerical data in Stata? ›

If you have a string variable and want to convert it to a numeric variable, you can use the encode command. If you have a string variable that has only numbers in it, then you can alternatively use the real() function.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5907

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.