# Converting a factor back into a variable.

#1

Dear all,
I had converted a scale variable into a factor in order to facilitate the visualisation of some data. The variable was Individual/Couple Annual Income (JntInc), and I converted it into a factor of 31 levels.
However, I wish to reconvert it back into a factor since I would like to do a correlation matrix along with some other variables which are numeric. Therefore I would like to ask if this is possible?
I have attached a sample of the data set with two variables: Alcohol Units Per Week (drating) and the annual income one.
I thank you in advance for your time and help. If there are changes that need to be made in the example I included, please inform me and I will do it immediately.

``````df <- data.frame(drating = c(0, -2, -2, 18.2125, 3.587, 0, -2,
0, 0, 0, -2, -2, 1.7155, 0.116, -2, -2, 0.058, 4.5, 0.808,
0.145), JntInc = c(3L, 3L, 3L, 19L, 19L, 19L, 19L, 8L, 18L,
18L, 18L, 18L, 21L, 21L, 21L, 21L, 6L, 6L, 19L, 19L))
``````

Created on 2019-04-09 by the reprex package (v0.2.1)
Best regards,
M

0 Likes

#2

Does this series of transformations illustrate what you want to do? The last sum() shows that all twenty values in df\$JntInc are equal to those in df\$JntIncNotFactor

``````df <- data.frame(drating = c(0, -2, -2, 18.2125, 3.587, 0, -2,
0, 0, 0, -2, -2, 1.7155, 0.116, -2, -2, 0.058,
4.5, 0.808, 0.145),
JntInc = c(3L, 3L, 3L, 19L, 19L, 19L, 19L, 8L, 18L,
18L, 18L, 18L, 21L, 21L, 21L, 21L, 6L, 6L, 19L, 19L))
df\$JntIncFactor <- factor(df\$JntInc)
df\$JntIncNotFactor <- as.numeric(as.character(df\$JntIncFactor))
sum(df\$JntInc == df\$JntIncNotFactor)
#> [1] 20
``````

Created on 2019-04-09 by the reprex package (v0.2.1)

2 Likes

#3

Perfect! This is exactly what I was looking for.
Many thanks!
Best regards,
M

0 Likes

#4

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

0 Likes

closed #5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes