0
$\begingroup$

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?

$\endgroup$
  • $\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
0
$\begingroup$

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]
| cite | improve this answer | |
$\endgroup$
  • $\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$ 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.