Closed
Description
I know how to avoid this error, but I donot know why this error happen if I run it in this way.
datain <- data.frame(
chrom = c("chr17", "chr4", "chr5", "chr13"),
map = c(81061047, 106061533, 40102442, 73791553),
rs = c("rs75954926", "rs7679673", "rs7708610", "rs78341008"),
start = c(79061048, 104061534, 38102443, 71791554),
end = c(83061048, 108061534, 42102443, 75791554)
)
datain
datain$chr<-datain$chrom
setDT(datain)
setkey(datain, chr, start, end)
datain
Thanks!
Alex
Activity
jaapwalhout commentedon Apr 10, 2019
I can reproduce this with data.table 1.12.0 on both Windows 10 and macOS 10.13.6. After upgrading my macOS system tot data.table 1.12.2 I can confirm that all examples below give the same result.
A somewhat simpler example:
This happens also when you assign the key directly with
setDT(which could be expected assetDTusessetkeyvto set the keys):As you can see the order of the
y-column is changed, while the order of the other columns is not. Apparently this is only happening when you copy a column the data.frame way, then usesetDTand then usesetkey. Consider the following five cases which all give the intended result:1) not copying a dataframe column
2) creating an arbitrary new column in the dataframe
3) creating a new column by sampling an existing column and pasting some extr charachters to it
4) creating a new column by just sampling an existing column
5) copying a column the data.table way
Although this bug seems to occur in a (very) specific usecase, this might be critical for me because I use
setkeyin several production systems. Will check whether this specific case occurs in my production code.Atrebas commentedon Apr 10, 2019
Confirming @jaapwalhout's observations (with data.table_1.12.3).
Also, (i) thought it might be related to factors but it's not (happens also with character or integer), (ii) it happens whatever the key (x or y).
Below, a simpler reproducible example.
The warning in the assignment may be related but I'm not sure to understand it.
Atrebas commentedon Apr 17, 2019
iiuc, when duplicating a column or list element without modification, the memory address will be the same and setting the key will "mess things up".
Works ok when using
copy().