Weird behavior of setcolorder
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
Research direction
Unzip df.zip to obtain df.rds and run the annotated script in the issue to reproduce the mismatch after data.table::setcolorder() is called on a data.frame. Start by inspecting the setcolorder entry point and its handling of data.frames, then compare the original and column-selected objects. Done means the reordered values and column names agree for this reproduction.
Written by the indexing model from the issue text.
Description
I have this weird behavior on a data.frame that is the result of RSQLite. I have attached it (df.zip) since this is the only way to reproduce the problem.
Please unzip before using it: github requires zipping the file.
Here is an annotated script that reproduces the problem with this file:
# load the data.frame
df <- readRDS('df.rds')
class(df)
# [1] "data.frame"
# there are many columns, but the first few are enough to show the problem
df[, 1:5]
# estate_id mod_timestamp cust_id cust_isOwner cust_publicInfo.name
# 1 -2147474620 1596557895 1575296 1 <NA>
# now I want to sort the columns (by reference?)
data.table::setcolorder(df, neworder = sort(colnames(df)))
# now let's look at the first few columns
df[, 1:5]
# estate_id mod_timestamp cust_id cust_isOwner cust_publicInfo.name
# 1 1575296 1 0 <NA> <NA>
# (!!) you see here that the fields have changed (i.e. sorted), but not the column names (!!)
# let us take a copy of this table by (!) selecting the columns this time (!)
df2 <- readRDS('df.rds')[, colnames(readRDS('df.rds'))]
# now do the same: order columns
data.table::setcolorder(df2, neworder = sort(colnames(df2)))
df2[, 1:5]
# cust_id cust_isOwner cust_publicInfo.hasIpiNo cust_publicInfo.ipiNo cust_publicInfo.name
# 1 1575296 1 0 <NA> <NA>
# (!!) now the columns and fields are ordered as they should (!!)
Any clue on what is going on here? Both readRDS('df.rds') and readRDS('df.rds')[, colnames(readRDS('df.rds'))] are identical, so I don't understand what is causing this column ordering discrepancy.
sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Dutch_Belgium.1252 LC_CTYPE=Dutch_Belgium.1252 LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C LC_TIME=Dutch_Belgium.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.3 tools_3.6.3 data.table_1.13.0
Note: converting df to data.table solve the problem, but begs the question. Why doesn't setcolorder() work with this specific data.frame.
- 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 ·