0

I am facing a few issues when implementing linear regressions using R. First, I would like to know how you can explicitly ask R to consider parameters as factors (for example, if I have a variable sex with 1 for Male and 0 for Female), assuming it does not recognize them as factors automatically.

Conversely, assuming R considers some parameters as factors whereas they are actually not, how can I tell R that those very parameters should not be regarded as factors?

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • 1
    Problem one: Turn them into factors with the `factor` function. Problem two: Fix your data import. If these values should be numeric, but are factors, data import is where the problem originates. – Roland Aug 23 '16 at 14:41
  • 1
    For the numeric that are read as factors, it's often best to figure out why this is happening and fix it during the reading of the dataset. Otherwise be careful that you don't use `as.numeric` directly on the factors as that will most likely fail. See [this answer](http://stackoverflow.com/a/3418192/2461552) – aosmith Aug 23 '16 at 14:41

1 Answers1

0

lm function should automatically use factors if they are represented as such in your data. If not convert the data to factor.

#First check your data structure:
mtcars <- mtcars
str(mtcars)

# convert to factor
mtcars$cyl <- factor(mtcars$cyl)
# convert back to numeric
mtcars$cyl <- as.numeric(as.character(mtcars$cyl))

As a side note R will convert strings to factors automatically when you import with say read.table() to provent that I always use stringsAsFactors = FALSE

USER_1
  • 2,409
  • 1
  • 28
  • 28