How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?

How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?

In R, numeric values can be converted into factors using the “factor()” function. This allows for the creation of categorical variables, which can be useful for various data analysis purposes. For example, converting a numerical variable representing different education levels (1 = high school, 2 = college, etc.) into a factor can help in analyzing the impact of education on different outcomes. Another example is converting numerical age values into age groups (18-25, 26-35, etc.) to better understand age-based trends or patterns. Factors also allow for easier data visualization and statistical analysis, such as creating bar charts or conducting ANOVA tests.

Convert Numeric to Factor in R (With Examples)


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 <- as.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 <- cut(df$numeric_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 <- data.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 <- as.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 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 <- cut(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.

Cite this article

stats writer (2024). How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-numeric-values-to-factors-in-r-and-what-are-the-uses-of-doing-so-can-you-provide-some-examples/

stats writer. "How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-numeric-values-to-factors-in-r-and-what-are-the-uses-of-doing-so-can-you-provide-some-examples/.

stats writer. "How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-numeric-values-to-factors-in-r-and-what-are-the-uses-of-doing-so-can-you-provide-some-examples/.

stats writer (2024) 'How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-numeric-values-to-factors-in-r-and-what-are-the-uses-of-doing-so-can-you-provide-some-examples/.

[1] stats writer, "How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I convert numeric values to factors in R, and what are the uses of doing so? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top