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

lookup modifies data in place #7

Closed
krlmlr opened this issue Sep 25, 2014 · 7 comments
Closed

lookup modifies data in place #7

krlmlr opened this issue Sep 25, 2014 · 7 comments

Comments

@krlmlr
Copy link
Contributor

krlmlr commented Sep 25, 2014

Something I would not expect from R. I really had to look twice, but unfortunately data.table doesn't seem to behave well here. #3 is related.

The code creates a lookup table from a larger data frame and checks that after lookup the order of the rows is unchanged. It isn't.

Find script in comment below.

@krlmlr
Copy link
Contributor Author

krlmlr commented Sep 25, 2014

There's a workaround: create a physical copy of subkey by e.g. coercing it to a matrix.

@krlmlr
Copy link
Contributor Author

krlmlr commented Sep 25, 2014

My bad -- the script is faulty. Will reopen as necessary.

@krlmlr krlmlr closed this as completed Sep 25, 2014
@krlmlr
Copy link
Contributor Author

krlmlr commented Sep 25, 2014

This one's better. If you uncomment the line with %l+%, no error is shown. It looks like y is scrambled in-place.

library(magrittr)
library(qdapTools)

set.seed(123)
N <- 100

key <- data.frame(x = sample.int(N, N), y = 1:N, z = 1:N)
key$w <- key$x
key %>% head()
##    x y z  w
## 1 29 1 1 29
## 2 79 2 2 79
## 3 41 3 3 41
## 4 86 4 4 86
## 5 91 5 5 91
## 6  5 6 6  5
terms <- sample.int(2 * N, 1e2, replace = TRUE)

subkey <- key[c("x", "y")]

terms %l+% subkey %>% head
## [1] 120  75  12 191  41 179
stopifnot(key$x == key$w)
stopifnot(key$y == key$z)
## Error: key$y == key$z are not all TRUE
key$y
##   [1]  74  35  51  18   6  54  86  46  15  56  30  88  47  38  40  60  87
##  [18]  99  72  39  17  44  71  43  42  55  19  67   1  65  84  36  58  94
##  [35]  90  98  89  59  53  68   3  10  96  28  77  69  33  97  37   7   9
##  [52]  70  92  26  22 100  52  95  48  13  63  73  32  83  82  85  75  31
##  [69]  49  57  64  21  50  66  34  80  16  20   2  93  25  61   8  29  24
##  [86]   4  11  45  27  23   5  76  62  14  81  78  41  12  79  91
key$z
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100
sessionInfo()
## R version 3.1.1 (2014-07-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] qdapTools_1.0.2         magrittr_1.0.1          ProjectTemplate_0.5-1.3
## 
## loaded via a namespace (and not attached):
##  [1] chron_2.3-45     data.table_1.9.2 evaluate_0.5.6   formatR_1.0     
##  [5] knitr_1.6.18     packrat_0.4.1-1  plyr_1.8.1       Rcpp_0.11.2     
##  [9] RCurl_1.95-4.3   reshape2_1.4     stringr_0.6.2    tools_3.1.1

@krlmlr krlmlr reopened this Sep 25, 2014
@krlmlr
Copy link
Contributor Author

krlmlr commented Sep 25, 2014

Reversing also helps: subkey <- key[c("x", "y")] %>% rev

@trinker
Copy link
Owner

trinker commented Sep 25, 2014

Hmm this can be problematic. I asked on SO. I hope there's an easy way to address this: http://stackoverflow.com/questions/26046143/cut-scoping-link-between-key-and-data-frame-for-data-table

@trinker
Copy link
Owner

trinker commented Sep 25, 2014

I think this has been fixed. If you feel it hasn't please re-open this issue. Thanks for the report.

@krlmlr
Copy link
Contributor Author

krlmlr commented Sep 25, 2014

Thanks for the quick fix!

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

No branches or pull requests

2 participants