Factor loses factor class when nrow(DT) == 1 and updated
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·