# How to convert nominal data to ratio/ interval data in R? [closed]

I know the question may seem stupid at first glance, but it's only because it's like R has a mind of its own, and doesn't let me in. :)

I did a questionnaire study, and one of the questions was 'For how many years have you been...?' I got 9 different numbers as answers, including both integers and decimals. I had designed the variable I named 'Years' to be a ratio or interval one. However, as I imported the data to R, it took it as a nominal variable with 9 levels. (By the way, the plan is to build a multiple linear regression model.)

How can I choose the type of variable I want to work with?

## closed as off-topic by kjetil b halvorsen, Michael Chernick, Xi'an, John, gung♦Jan 5 '17 at 12:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – kjetil b halvorsen, Xi'an, John
If this question can be reworded to fit the rules in the help center, please edit the question.

• how many years as decimal, are you sure? – Erik Hambardzumyan Jan 5 '17 at 2:01
• Yeah, e.g. 2.5, 0.5 etc. – fannilegoza Jan 5 '17 at 2:02
• to the extent of my understanding you want to make it numerical? – Erik Hambardzumyan Jan 5 '17 at 2:02
• Yes, I want to make R treat it as a numerical variable instead of a nominal one. – fannilegoza Jan 5 '17 at 2:03
• try the given code – Erik Hambardzumyan Jan 5 '17 at 2:09

It is worth trying as.numeric (This a diagnosis_result variable from a Training dataset; the same fashion will apply for your variable and dataset)

Train$diagnosis_result<-as.numeric(Train$diagnosis_result)


(the code above is actually the remedy)

as for special cases when you factorize the variable in the desired way

Train$diagnosis_result<-factor(Train$diagnosis_result, levels = c("B", "M"), labels = c(0,1))

Train$diagnosis_result<-as.numeric(levels(Train$diagnosis_result))[Train$diagnosis_result]  • @fannilegoza check str(Train$your_variable) to see the type, if it is num, then you reached your goal – Erik Hambardzumyan Jan 5 '17 at 2:14
• Already done so :) – fannilegoza Jan 5 '17 at 2:15
• @fannilegoza just for you to know it is generally not reccomended to express graditude in the comments or in general the more acceptable and general way is just to accept the answe( the tick below the upvotes) or just upvote the answer as useful – Erik Hambardzumyan Jan 5 '17 at 2:18
• @fannilegoza be careful not be critisized by the community in the future, welcome to the cross validated. – Erik Hambardzumyan Jan 5 '17 at 2:20
• Okay. I'm new here. Then not-thanks for the advice. ;) – fannilegoza Jan 5 '17 at 2:20