Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coerceAs/memrecycle coerce number to factor #5027

Open
jangorecki opened this issue May 26, 2021 · 1 comment
Open

coerceAs/memrecycle coerce number to factor #5027

jangorecki opened this issue May 26, 2021 · 1 comment

Comments

@jangorecki
Copy link
Member

jangorecki commented May 26, 2021

Currently coerceAs and memrecycle when coercing to factor interprets number as a integer level

coerceAs(0L, as=factor(0L))
#Error in coerceAs(0L, as = factor(0L)) : 
#  Assigning factor numbers to target vector. But 0 is outside the level range [1,1]

This is different to how base R coerce works

factor(0L)
#[1] 0
#Levels: 0
as.factor(0L)
#[1] 0
#Levels: 0
unclass(as.factor(0L))
#[1] 1
#attr(,"levels")
#[1] "0"

Currently what is necessary is to coerce number to character to match base R results

coerceAs("0", as=factor(0L))
#[1] 0
#Levels: 0

Question is do we want to make it more consistent to base R.
Then situation like this could just work

dt = data.table(x=1:2, y=factor(1:2))
dt
#       x      y
#   <int> <fctr>
#1:     1      1
#2:     2      2
dt[1L, c("x","y") := 0L]
##Error in `[.data.table`(dt, 1L, `:=`(c("x", "y"), 0L)) : 
#  Assigning factor numbers to column 2 named 'y'. But 0 is outside the level range [1,2]
dt
#       x      y
#   <int> <fctr>
#1:     0      1
#2:     2      2

It came up during nafill(dt_of_mixed_types, fill=0L) where fill argument is automatically coerced to a type matching for each column.
Looking forward for comments.

@jangorecki
Copy link
Member Author

setting High as it blocks #4980, we could possibly proceed now without factor support there and come back to factor once this will be resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant