Factor loses factor class when nrow(DT) == 1 and updated

Open
#5,607 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the issue's one-row and two-row R reproductions with data.table, then trace the := update path highlighted by the verbose output. Done means the one-row update retains the factor class and original levels, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Description

bug consistency

data.table unexpectedly, at least to me, drops a variable's factor class when there is only one row in the data.table and the variable is updated, even if the new value is a defined level of the original factor.

> dt <- data.table(x = c(TRUE, TRUE), y = factor(c(1, 2), levels = 1:3))
> dt[(x), y := 2, verbose = TRUE]
Detected that j uses these columns: [y]
Assigning to 2 row subset of 2 rows
RHS_list_of_columns == false
> dt
        x      y
   <lgcl> <fctr>
1:   TRUE      2
2:   TRUE      2

> dt <- data.table(x = c(TRUE), y = factor(c(1), levels = 1:3))
> dt[(x), y := 2, verbose = TRUE]
Detected that j uses these columns: [y]
Assigning to all 1 rows
RHS_list_of_columns == false
RHS for item 1 has been duplicated because NAMED==4 MAYBE_SHARED==1, but then is being plonked. length(values)==1; length(cols)==1)
> dt
        x     y
   <lgcl> <num>
1:   TRUE     2

This is causing problems for me as my downstream code expects to find the original levels from the variable. For what it is worth, this also happens with character variables as well.

> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.9 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10       compiler_4.2.1    later_1.3.0       urlchecker_1.0.1  prettyunits_1.1.1 profvis_0.3.7    
 [7] remotes_2.4.2     tools_4.2.1       digest_0.6.30     pkgbuild_1.3.1    pkgload_1.3.0     memoise_2.0.1    
[13] lifecycle_1.0.3   rlang_1.0.6       shiny_1.7.2       cli_3.6.0         rstudioapi_0.14   fastmap_1.1.0    
[19] stringr_1.5.0     fs_1.5.2          htmlwidgets_1.5.4 vctrs_0.5.2       devtools_2.4.5    glue_1.6.2       
[25] R6_2.5.1          processx_3.7.0    sessioninfo_1.2.2 callr_3.7.2       purrr_1.0.1       magrittr_2.0.3   
[31] ps_1.7.1          promises_1.2.0.1  ellipsis_0.3.2    htmltools_0.5.3   usethis_2.1.6     mime_0.12        
[37] xtable_1.8-4      httpuv_1.6.6      stringi_1.7.12    miniUI_0.1.1.1    cachem_1.0.6      crayon_1.5.2 

I searched through all the issues and couldn't find any that seemed to be similar. I think #2403 and #3922 are at least somewhat related, but my issue is that the factor levels are being dropped completely. Sorry if I missed something.

Thanks!

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.