Using setDT after loading

Open
#4,857 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the supplied R script using setDT, setalloccol, and := after save/load, starting from the documented behavior of these entry points. Determine whether the observed mutation is expected or a data.table defect, then add a focused regression test or clarify the behavior in the project’s documentation.

Written by the indexing model from the issue text.

Description

by-reference

Thanks for the data.table package. I'm a new user and I've found it useful so far, but one thing I'm having trouble with is using setDT or setalloccol after loading from disk, in order to be able to use :=. I use those functions but := still appears not to work. I may be misunderstanding how those functions are meant to be used.

Example:

library(data.table)

L <- vector("list", length = 2)
names(L) <- c("A1", "A2")

for (A in c("A1", "A2")) {
  L[[A]] <- vector("list", length = 2)
  names(L[[A]]) <- c("B1", "B2")
}

for (A in c("A1", "A2")) {
  for (B in c("B1", "B2")) {
    L[[A]][[B]] <- data.table(u = 1:3, v = 2:4)
  }
}

L[["A1"]][["B1"]][, w := 0]  # this works

L

## $A1
## $A1$B1
##    u v w
## 1: 1 2 0
## 2: 2 3 0
## 3: 3 4 0

## $A1$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4


## $A2
## $A2$B1
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

## $A2$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

save(L, file = "~/L.RData")

rm(L)

load("~/L.RData")

for (A in c("A1", "A2")) {
  for (B in c("B1", "B2")) {
    setDT(L[[A]][[B]])
  }
}

L[["A1"]][["B1"]][, x := 99]

L  # x doesn't appear

## $A1
## $A1$B1
##    u v w
## 1: 1 2 0
## 2: 2 3 0
## 3: 3 4 0

## $A1$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4


## $A2
## $A2$B1
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

## $A2$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

# Try using setalloccol instead of setDT

for (A in c("A1", "A2")) {
  for (B in c("B1", "B2")) {
    setalloccol(L[[A]][[B]])
  }
}

L[["A1"]][["B1"]][, x := 99]

L  # x doesn't appear

## $A1
## $A1$B1
##    u v w
## 1: 1 2 0
## 2: 2 3 0
## 3: 3 4 0

## $A1$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4


## $A2
## $A2$B1
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

## $A2$B2
##    u v
## 1: 1 2
## 2: 2 3
## 3: 3 4

sessionInfo()

## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)

## Matrix products: default

## locale:
## [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
## [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
## [5] LC_TIME=English_Australia.1252

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base

## other attached packages:
## [1] data.table_1.13.4

## loaded via a namespace (and not attached):
## [1] compiler_4.0.3

Please let me know if I've misunderstood something. Thank you.

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.