6.2 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Convert Numeric to Factor in R (With Examples)

How to Convert Numeric to Factor in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are two methods you can use to convert a numeric variable to a factor variable in R:

Method 1: Use as.factor()

df$factor_variable factor(df$numeric_variable)

This will convert the numeric variable to a factor variable with the number of levels equal to the number of unique values in the original numeric variable.

Method 2: Use cut()

df$factor_variable 3, labels=c('lab1', 'lab2', 'lab3'))

This particular example will convert the numeric variable to a factor variable by “cutting” the numeric variable at 3 equally distanced values.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'C', 'D'),
                 points=c(12, 15, 22, 29, 35, 24, 11, 24))

#view data frame
df

  team points
1    A     12
2    A     15
3    B     22
4    B     29
5    C     35
6    C     24
7    C     11
8    D     24

#view structure of data frame
str(df)

'data.frame':	8 obs. of  2 variables:
 $ team  : chr  "A" "A" "B" "B" ...
 $ points: num  12 15 22 29 35 24 11 24

Example 1: Convert Numeric to Factor Using as.factor()

The following code shows how to use as.factor() to convert the points column from numeric to factor:

#convert points column from numeric to factor
df$points factor(df$points)

#view updated data frame
df

  team points
1    A     12
2    A     15
3    B     22
4    B     29
5    C     35
6    C     24
7    C     11
8    D     24

#view updated structure of data frame
str(df)

'data.frame':	8 obs. of  2 variables:
 $ team  : chr  "A" "A" "B" "B" ...
 $ points: Factor w/ 7 levels "11","12","15",..: 2 3 4 6 7 5 1 5

By using the str() function to view the structure of the data frame, we can see that the points column is now a factor with 7 different levels representing the 7 unique numeric values in the column.

Example 2: Convert Numeric to Factor Using cut()

The following code shows how to use cut() to convert the points column from a numeric variable to a factor variable with 3 levels:

#convert points column from numeric to factor with three levels
df$points 3, labels=c('OK', 'Good', 'Great'))

#view updated data frame
df

  team points
1    A     OK
2    A     OK
3    B   Good
4    B  Great
5    C  Great
6    C   Good
7    C     OK
8    D   Good

#view updated structure of data frame
str(df)

'data.frame':	8 obs. of  2 variables:
 $ team  : chr  "A" "A" "B" "B" ...
 $ points: Factor w/ 3 levels "OK","Good","Great": 1 1 2 3 3 2 1 2

From the output we can see that the points variable has been converted from a numeric variable to a factor variable with three levels and the following labels:

  • “OK”
  • “Good”
  • “Great”

Note that we chose to use three levels in this example, but feel free to cut the numeric variable into as many levels as you’d like by changing the 3 in the cut() function to another value.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert Numeric to Character in R
How to Convert Factor to Numeric in R
How to Convert Factor to Character in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories