I'm trying to do one hot encoding on a data set containing 4 categorical features in R. Unique levels per feature: 400, 60, 6, 5, respectively. I get the following error during the first call to the ohe function (acm.disjonctif):

Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?

I can't do sorting on a per column basis because it will remove the data relationship between columns. What am I missing in understanding? I searched on the web but couldn't find anything helpful.

Here is the code I'm running in RStudio:

ohe_feats = c('RSN_CD', 'RSN_CAT', 'ELIG_SYS_CD', 'PROD_CD')
for (f in ohe_feats) {
    df_all_dummy = acm.disjonctif(as.factor(nodups[f]))   # package = ade4
    nodups[f] = NULL
    nodups = cbind(nodups, df_all_dummy)
  • $\begingroup$ What is nodups? And any particular reason you doing this in a loop and then cbinding the results together? $\endgroup$ – Peter Ellis Feb 1 '18 at 2:50

I may be missing the point, but you don't need to do this one variable at a time in a loop and then bind the columns together - that's not the R way. Here's an alternative:

# create some simulated data
n <- 10000
X <- data.frame(f_a_ = factor(sample(1:400, n, replace = TRUE)),
                f_b_ = factor(sample(1:60, n, replace = TRUE)),
                f_c_ = factor(sample(1:6, n, replace = TRUE)),
                f_d_ = factor(sample(1:5, n, replace = TRUE)),
                y = rep(1, n))

# use R's inbuilt understanding of formulas to do the "one hot encoding" for you
x_onehot <- model.matrix(y ~ ., data = X)                
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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