Why does assignment to reference (:=) give different results when running code say two or three times within data.table format?

strauss

I thank all of you for your precious time. I would like to have a table with the first column with no lag, the second column with one lag down, the third column with two lags down, etc., as in the picture. I have code that actually does the job but only if you run the code once. If you run the code two or three times you will notice that additional shifts (lags) occur. I would be very grateful if any of you could help me get the same result no matter how many times the code is run.

Screenshot:

library(dplyr)
library(data.table)
d <- structure(list(inv = c(20, 18, 46, 28, 10, 17, 96, 37), c = c(1, 0.999735, 0.912009, 0.596777, 
  0.293534, 0.122216, 0.046563, 0.016929)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"))
G <- matrix(d$inv)
W <- matrix(d$c)
K <- cbind(W%x%t(G))
dtK <- data.table(K)
z <-  dtK[, names(dtK) := Map(shift, .SD, seq_along(.SD)-1L, fill = 0), .SDcols = names(dtK)]
z
Ian Campbell

The := operator assigns by reference within the dtK object.

That means the original dtK object is changed regardless of whether you assign it to z or not.

Try running just the following and seeing what happens:

dtK[, names(dtK) := Map(shift, .SD, seq_along(.SD)-1L, fill = 0), .SDcols = names(dtK)]
dtK
         V1        V2        V3        V4       V5       V6       V7 V8
1: 20.00000  0.000000  0.000000  0.000000  0.00000  0.00000  0.00000  0
2: 19.99470 18.000000  0.000000  0.000000  0.00000  0.00000  0.00000  0
3: 18.24018 17.995230 46.000000  0.000000  0.00000  0.00000  0.00000  0
4: 11.93554 16.416162 45.987810 28.000000  0.00000  0.00000  0.00000  0
5:  5.87068 10.741986 41.952414 27.992580 10.00000  0.00000  0.00000  0
6:  2.44432  5.283612 27.451742 25.536252  9.99735 17.00000  0.00000  0
7:  0.93126  2.199888 13.502564 16.709756  9.12009 16.99550 96.00000  0
8:  0.33858  0.838134  5.621936  8.218952  5.96777 15.50415 95.97456 37

One way out of this problem is to make a copy with copy:

dtK <- data.table(K)
z <- copy(dtK)[, names(dtK) := Map(shift, .SD, seq_along(.SD)-1L, fill = 0), .SDcols = names(dtK)]
dtK
         V1        V2        V3        V4       V5        V6        V7        V8
1: 20.00000 18.000000 46.000000 28.000000 10.00000 17.000000 96.000000 37.000000
2: 19.99470 17.995230 45.987810 27.992580  9.99735 16.995495 95.974560 36.990195
3: 18.24018 16.416162 41.952414 25.536252  9.12009 15.504153 87.552864 33.744333
4: 11.93554 10.741986 27.451742 16.709756  5.96777 10.145209 57.290592 22.080749
5:  5.87068  5.283612 13.502564  8.218952  2.93534  4.990078 28.179264 10.860758
6:  2.44432  2.199888  5.621936  3.422048  1.22216  2.077672 11.732736  4.521992
7:  0.93126  0.838134  2.141898  1.303764  0.46563  0.791571  4.470048  1.722831
8:  0.33858  0.304722  0.778734  0.474012  0.16929  0.287793  1.625184  0.626373

Check out vignette("datatable-reference-semantics") for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does these two code pieces give different results

Why does using assignment instead of append give me different results while the reference copies of sublist

Why does these three examples give different results? They seem to be equivalent

Why does VBA say "(Not Responding)" even when code is running fine?

Why does this code have two different results?

Why does this code outputs two different results?

Why does this regex expression give two different results in Python and Javascript?

Why does only `case_when` give different results in R?

Why do these three comparisons give different results?

Why do the two functions give different results?

Why does this code return different results when use different programs?

Why does my code say "Yes" when it should say "No"?

Why does doing variable assignment inside mapply's MoreArgs argument give different results than doing it inside its "..." argument?

Why does hostname -i give different results on different machines?

Why does Server.MapPath("~") give different results in different controllers?

Why Does Adding Bytes in Different Ways Give Different Results?

Why does my custom streaming metric always give different results when run on the same inputs?

Why does using with rollup give me different results when inside pivot are using sum and pivot and left join?

Why does subsetting a column from a data frame vs. a tibble give different results

There should be only two results, but why is it repeated three times?

Why do two interpreter give results different from each other?

Why do these two multiplication operations give different results?

Why do these two golang integer conversion functions give different results?

Why These two c# scripts give different results?

Why do these two GREP commands give different results?

Why do these two find-commands give different results?

Why two independent sample t-test and two-way ANOVA give different results on same data set?

Different results when running the function multiple times - C

Why this code (with OpenMP in MEX-file of Matlab) give different results?