strengejacke/sjmisc

to_dummy() doesn't label characters as expected

tkoomar opened this issue · 0 comments

When using to_dummy() with a factor, new column names are properly assigned:

# tibble(x = c("yes", "no", "yes", "maybe") %>% as.factor ) %>%  to_dummy(suffix = "label")
# x_maybe x_no x_yes
# 1       0    0     1
# 2       0    1     0
# 3       0    0     1
# 4       1    0     0

Columns are not correct if the input is a character, however:

tibble(x = c("yes", "no", "yes", "maybe")) %>%  to_dummy(suffix = "label")
#  x_yes x_no x_maybe
# 1     0    0       1
# 2     0    1       0
# 3     0    0       1
# 4     1    0       0

Right now, the user has no indication that what they are getting back is not what they expect. Given that the default behavior of readr functions (like read_csv()) is to import as character, this feels a bit dangerous.