0

I am using Arthritis data set from vcd package to perform one hot encoding. I would like to usepurrr::dmap_if and Matrix::model.matrix together to to do this. When I run

do.call(model.matrix, list(Improved~.,Arthritis))

working fine.

When I use below code is not working

Arthritis %>% dmap_if(is.factor, do.call(model.matrix, list(Improved~., .)))
BigDataScientist
  • 1,045
  • 5
  • 17
  • 37
  • 1
    How about `keep` and `invoke` instead of `dmap_if`? `Arthritis %>% keep(is.factor) %>% invoke(model.matrix, Improved~., data = .)`. The `invoke` function is just a wrapper around `do.call` to make it easy to use in a pipe. – aosmith Jun 22 '16 at 16:57
  • Thank you, could you help me on how to get all levels, without having an intercept. It is dropping one level for each variable. – BigDataScientist Jun 22 '16 at 17:48
  • You can remove the intercept using `-1` in the formula of `model.matrix`. However, that likely is still not quite what you want. You might ask a new question specifically about how to get what you want out of `model.matrix` (or, more generally, a question about what result you need and why). – aosmith Jun 22 '16 at 18:08

1 Answers1

1

From what I can tell, dmap_if goes through the columns of the dataset one a time and so doesn't necessarily seem like the right tool for this task.

If you want to use only the factor variables before applying a function, consider keep. Once you've selected just the factor variables using keep you can use invoke to apply model.matrix. The invoke function is just a wrapper for do.call that works nicely with pipes.

Arthritis %>% 
    keep(is.factor) %>% 
    invoke(model.matrix, Improved~., data = .)

   (Intercept) TreatmentTreated SexMale
1            1                1       1
2            1                1       1
3            1                1       1
4            1                1       1
5            1                1       1
6            1                1       1
7            1                1       1
8            1                1       1
9            1                1       1
10           1                1       1
11           1                1       1
12           1                1       1
13           1                1       1
14           1                1       1
15           1                1       0
16           1                1       0
...
aosmith
  • 34,856
  • 9
  • 84
  • 118