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

rbind and rbindlist drop factor levels when data.table has zero rows #3508

Closed
gdemin opened this issue Apr 15, 2019 · 0 comments · Fixed by #3586
Closed

rbind and rbindlist drop factor levels when data.table has zero rows #3508

gdemin opened this issue Apr 15, 2019 · 0 comments · Fixed by #3586
Milestone

Comments

@gdemin
Copy link

gdemin commented Apr 15, 2019

In the previous data.table versions (before 1.12.2) factor levels were preserved during rbind even if data.table has zero rows. See example:
# Minimal reproducible example

library(data.table)

DT = data.table(f = factor(c("a", "b", "c")))

# all ok, rbind works as expected
str(rbind(DT, DT))
# Classes ‘data.table’ and 'data.frame':	6 obs. of  1 variable:
#     $ f: Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3
# - attr(*, ".internal.selfref")=<externalptr> 
zeroDT = DT[FALSE,]
str(zeroDT)
# Classes ‘data.table’ and 'data.frame':	0 obs. of  1 variable:
#     $ f: Factor w/ 3 levels "a","b","c": 
#     - attr(*, ".internal.selfref")=<externalptr> 

# zero rows data.table
# unexpected 0 factor levels
str(rbind(zeroDT, zeroDT))
# Classes ‘data.table’ and 'data.frame':	0 obs. of  1 variable:
#     $ f: Factor w/ 0 levels: 
#     - attr(*, ".internal.selfref")=<externalptr> 
str(rbindlist(list(zeroDT, zeroDT)))
# Classes ‘data.table’ and 'data.frame':	0 obs. of  1 variable:
#     $ f: Factor w/ 0 levels: 
#     - attr(*, ".internal.selfref")=<externalptr>

# Output of sessionInfo()

R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19.1

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
[1] LC_CTYPE=ru_RU.UTF-8       LC_NUMERIC=C               LC_TIME=ru_RU.UTF-8        LC_COLLATE=ru_RU.UTF-8    
[5] LC_MONETARY=ru_RU.UTF-8    LC_MESSAGES=ru_RU.UTF-8    LC_PAPER=ru_RU.UTF-8       LC_NAME=C                 
[9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=C       

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

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

loaded via a namespace (and not attached):
    [1] compiler_3.5.3 tools_3.5.3    packrat_0.5.0 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants