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.

  • $\begingroup$ how many years as decimal, are you sure? $\endgroup$ – Erik Hambardzumyan Jan 5 '17 at 2:01
  • $\begingroup$ Yeah, e.g. 2.5, 0.5 etc. $\endgroup$ – fannilegoza Jan 5 '17 at 2:02
  • $\begingroup$ to the extent of my understanding you want to make it numerical? $\endgroup$ – Erik Hambardzumyan Jan 5 '17 at 2:02
  • $\begingroup$ Yes, I want to make R treat it as a numerical variable instead of a nominal one. $\endgroup$ – fannilegoza Jan 5 '17 at 2:03
  • $\begingroup$ try the given code $\endgroup$ – 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)


(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))

  • $\begingroup$ @fannilegoza check str(Train$your_variable) to see the type, if it is num, then you reached your goal $\endgroup$ – Erik Hambardzumyan Jan 5 '17 at 2:14
  • $\begingroup$ Already done so :) $\endgroup$ – fannilegoza Jan 5 '17 at 2:15
  • $\begingroup$ @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 $\endgroup$ – Erik Hambardzumyan Jan 5 '17 at 2:18
  • $\begingroup$ @fannilegoza be careful not be critisized by the community in the future, welcome to the cross validated. $\endgroup$ – Erik Hambardzumyan Jan 5 '17 at 2:20
  • $\begingroup$ Okay. I'm new here. Then not-thanks for the advice. ;) $\endgroup$ – fannilegoza Jan 5 '17 at 2:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.