0

I'd be grateful if someone could explain why, in the example below, the first dt does not print to the screen, but the second one does.

df <- data.frame(V1=c(1,2),V2=c(11,12))
dt <- data.table(V3=c(4,5),V4=c(44,45))

change <- function(dt,df) {
  dt[, V4 := NULL]
  df <- df[,-2]
}

change(dt,df)

dt  # First dt, nothing printed

dt  # Second dt, contents printed as expected
   V3
1:  4
2:  5

If I swap the order of the commands in change() so that

change <- function(dt,df) {
  df <- df[,-2]
  dt[, V4 := NULL]
}

or if I remove the df line so that

change <- function(dt,df) {
  dt[, V4 := NULL]
}

the unexpected behaviour disappears as the first dt prints its contents to the screen.

I'm using R version 3.3.2 (2016-10-31) and with data.table version 1.9.6.

Paul
  • 55
  • 1
  • 3
  • 1
    Sorry Frank, I've removed them now. – Paul Mar 19 '17 at 18:01
  • Frank, thanks for pointing me to [https://stackoverflow.com/questions/32988099/data-table-objects-not-printed-after-returned-from-function], which I missed. After reading the other post I tried adding a `[]` after the `dt[, V4 := NULL]`. It fixes the problem in both data.table versions 1.9.6 and 1.10.4. – Paul Mar 19 '17 at 18:12

0 Answers0