I would like to convert numbers into different categorical levels . For example,
In one of column of a dataframe, there are numbers: 1,2,3, 4. How can I cover them into "bottom", "middle", "high" , "top" in R codes?
Thanks, JL
I would like to convert numbers into different categorical levels . For example,
In one of column of a dataframe, there are numbers: 1,2,3, 4. How can I cover them into "bottom", "middle", "high" , "top" in R codes?
Thanks, JL
You can use factors (see ?factors
):
set.seed(42)
x <- sample(4, 15, replace=TRUE)
x
# [1] 1 1 1 1 2 4 2 2 1 4 3 4 3 4 1
table(x)
# x
# 1 2 3 4
# 6 3 2 4
y <- factor(x, labels=c("bottom", "middle", "high", "top"))
y
# [1] bottom bottom bottom bottom middle top middle middle bottom top high top high top bottom
# Levels: bottom middle high top
table(y)
# y
# bottom middle high top
# 6 3 2 4