0

I got the problem to convert text variable to numeric variable. I think I successful construct the dummy variable with several levels. However, when i try to read the datafile, the categories of these variable are still text..

here is my code, output, and datafile:

datafile after recording

my code

output

after that, when I try to generate the correlation value, it still shows error:

Error in cor(hpNumeric) : 'x' must be numeric

Any suggestions are appreciated !

Steve Shi
  • 15
  • 5
  • 3
    Welcome to Stackoverflow, please don't post images. `Provide minimal, reproducible, representative example(s) along with the desired end result. Use dput() for data and specify all non-base packages with library calls. Do not embed pictures for data or code, use indented code blocks` – Khaynes Jan 19 '19 at 21:33
  • Also see [reprex](https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html) and [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – alistaire Jan 19 '19 at 21:44

1 Answers1

0

It'd be best to see your code directly, rather than having to click on links to download things. If I understand your code correctly (it's not fully visible and your "output" is not the output of a summary(lm...) command), you've over-engineered the issue.

Your first line already codes your variable as a factor, which will tell R to automatically create dummy variables when you carry out regressions, etc. What you're doing by applying the contrast matrix is to transform again that factor into raw numeric. This is all you need really:

df$x <- factor(<your variable data here as in your example>) 
m <- lm(y ~ x ..., data = df)

If you really want to see your variable as an integer, you could use as.integer but I think you shouldn't complicate things like that.

Have a look at the data types in R for more information on factors.

Fons MA
  • 1,142
  • 1
  • 12
  • 21