@@ -12,21 +12,23 @@ addresses = function(x) vapply(x, address, "")
1212
1313# cbindlist
1414
15- l = list(
16- d1 = data.table(x=1:3, v1=1L),
17- d2 = data.table(y=3:1, v2=2L),
18- d3 = data.table(z=2:4, v3=3L)
19- )
20- ans = cbindlist(l)
21- expected = data.table(l$d1, l$d2, l$d3)
22- test(11.01, ans, expected)
23- test(11.02, intersect(addresses(ans), addresses(l)), character())
24- ans = cbindlist(l, copy=FALSE)
25- expected = setDT(c(l$d1, l$d2, l$d3))
26- test(11.03, ans, expected)
27- test(11.04, length(intersect(addresses(ans), addresses(l))), ncol(expected))
15+ local({
16+ l = list(
17+ d1 = data.table(x=1:3, v1=1L),
18+ d2 = data.table(y=3:1, v2=2L),
19+ d3 = data.table(z=2:4, v3=3L)
20+ )
21+ ans = cbindlist(l)
22+ expected = data.table(l$d1, l$d2, l$d3)
23+ test(11.01, ans, expected)
24+ test(11.02, intersect(addresses(ans), addresses(l)), character())
25+ ans = cbindlist(l, copy=FALSE)
26+ expected = setDT(c(l$d1, l$d2, l$d3))
27+ test(11.03, ans, expected)
28+ test(11.04, length(intersect(addresses(ans), addresses(l))), ncol(expected))
29+ })
30+
2831test(11.05, cbindlist(list(data.table(a=1L), data.table(), data.table(d=2L), data.table(f=3L))), data.table(a=1L,d=2L,f=3L))
29- rm(expected)
3032## codecov
3133test(12.01, cbindlist(data.frame(a=1L), data.frame(b=1L)), error="must be a list")
3234test(12.02, cbindlist(TRUE, FALSE), error="must be a list")
@@ -38,9 +40,11 @@ test(12.07, cbindlist(list(data.table(), data.table(a=1:2), list(b=1:2))), data.
3840test(12.08, cbindlist(list(data.table(a=integer()), list(b=integer()))), data.table(a=integer(), b=integer()))
3941## duplicated names
4042test(12.09, cbindlist(list(data.table(a=1L, b=2L), data.table(b=3L, d=4L))), data.table(a=1L, b=2L, b=3L, d=4L))
41- ans = cbindlist(list(setindexv(data.table(a=2:1, b=1:2),"a"), data.table(a=1:2, b=2:1, key="a"), data.table(a=2:1, b=1:2)))
42- test(12.10, ans, data.table(a=2:1, b=1:2, a=1:2, b=2:1, a=2:1, b=1:2))
43- test(12.11, indices(ans), NULL)
43+ local({
44+ ans = cbindlist(list(setindexv(data.table(a=2:1, b=1:2),"a"), data.table(a=1:2, b=2:1, key="a"), data.table(a=2:1, b=1:2)))
45+ test(12.10, ans, data.table(a=2:1, b=1:2, a=1:2, b=2:1, a=2:1, b=1:2))
46+ test(12.11, indices(ans), NULL)
47+ })
4448## recycling, first ensure cbind recycling that we want to match to
4549test(12.12, cbind(data.table(x=integer()), data.table(a=1:2)), data.table(x=c(NA_integer_,NA), a=1:2))
4650test(12.13, cbind(data.table(x=1L), data.table(a=1:2)), data.table(x=c(1L,1L), a=1:2))
@@ -50,26 +54,30 @@ test(12.16, cbindlist(list(data.table(a=integer()), data.table(b=1:2)), copy=FAL
5054test(12.17, cbindlist(list(data.table(a=1L), data.table(b=1:2)), copy=FALSE), error="has to have equal nrow")
5155
5256## retain indices
53- d = data.table(x=1:2, y=2:1, z=2:1, v1=1:2) ## ensure setDT will retain key and indices when it is called on the list, bc Ccbindlist returns list
54- setkeyv(d, "x"); setindexv(d, list("y", "z"))
55- a = attributes(d)
56- attributes(d) = a[!names(a) %in% c("class",".internal.selfref","row.names")]
57- test(13.01, class(d), "list")
58- setDT(d)
59- test(13.02, key(d), "x")
60- # test(13.03, hasindex(d, "y") && hasindex(d, "z"))
61- l = list(
62- data.table(id1=1:5, id2=5:1, id3=1:5, v1=1:5),
63- data.table(id4=5:1, id5=1:5, v2=1:5),
64- data.table(id6=5:1, id7=1:5, v3=1:5),
65- data.table(id8=5:1, id9=5:1, v4=1:5)
66- )
67- setkeyv(l[[1L]], "id1"); setindexv(l[[1L]], list("id1", "id2", "id3", c("id1","id2","id3"))); setindexv(l[[3L]], list("id6", "id7")); setindexv(l[[4L]], "id9")
68- ii = lapply(l, indices)
69- ans = cbindlist(l)
70- test(13.04, key(ans), "id1")
71- test(13.05, indices(ans), c("id1","id2","id3","id1__id2__id3","id6","id7","id9"))
72- test(13.06, ii, lapply(l, indices)) ## this tests that original indices have not been touched, shallow_duplicate in mergeIndexAttrib
73- ans = cbindlist(list(data.table(a=1:2), data.table(b=3:4, key="b")))
74- test(13.07, ans, data.table(a=1:2, b=3:4, key="b"))
75- test(13.08, key(ans), "b")
57+ local({
58+ d = data.table(x=1:2, y=2:1, z=2:1, v1=1:2) ## ensure setDT will retain key and indices when it is called on the list, bc Ccbindlist returns list
59+ setkeyv(d, "x"); setindexv(d, list("y", "z"))
60+ a = attributes(d)
61+ attributes(d) = a[!names(a) %in% c("class",".internal.selfref","row.names")]
62+ test(13.01, class(d), "list")
63+ setDT(d)
64+ test(13.02, key(d), "x")
65+ # test(13.03, hasindex(d, "y") && hasindex(d, "z"))
66+ })
67+ local({
68+ l = list(
69+ data.table(id1=1:5, id2=5:1, id3=1:5, v1=1:5),
70+ data.table(id4=5:1, id5=1:5, v2=1:5),
71+ data.table(id6=5:1, id7=1:5, v3=1:5),
72+ data.table(id8=5:1, id9=5:1, v4=1:5)
73+ )
74+ setkeyv(l[[1L]], "id1"); setindexv(l[[1L]], list("id1", "id2", "id3", c("id1","id2","id3"))); setindexv(l[[3L]], list("id6", "id7")); setindexv(l[[4L]], "id9")
75+ ii = lapply(l, indices)
76+ ans = cbindlist(l)
77+ test(13.04, key(ans), "id1")
78+ test(13.05, indices(ans), c("id1","id2","id3","id1__id2__id3","id6","id7","id9"))
79+ test(13.06, ii, lapply(l, indices)) ## this tests that original indices have not been touched, shallow_duplicate in mergeIndexAttrib
80+ ans = cbindlist(list(data.table(a=1:2), data.table(b=3:4, key="b")))
81+ test(13.07, ans, data.table(a=1:2, b=3:4, key="b"))
82+ test(13.08, key(ans), "b")
83+ })
0 commit comments