Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/IDateTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ as.IDate.numeric = function(x, origin = "1970-01-01", ...) {
x = as.integer(x)
class(x) = c("IDate", "Date")
# We used to use structure() here because class(x)<- copied several times in R before v3.1.0
# Since R 3.1.0 improved class()<- and data.table's oldest oldest supported R is now 3.1.0, we can use class<- again
# Since R 3.1.0 improved class()<- and data.table's oldest oldest supported R is now >3.1.0, we can use class<- again
# structure() contains a match() and replace for specials, which we don't need.
# class()<- ensures at least 1 shallow copy as appropriate is returned.
x
Expand Down
8 changes: 7 additions & 1 deletion R/fdroplevels.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# 647 fast droplevels.data.table method
fdroplevels = function(x, exclude = if (anyNA(levels(x))) NULL else NA, ...) {
stopifnot(inherits(x, "factor"))
if (!length(x)) return(structure(integer(), class='factor', levels=character())) # skip factor() overhead
if (!length(x)) {
# skip factor() overhead
ret = integer()
class(ret) = 'factor'
attr(ret, 'levels') = character()
return(ret)
}
lev = which(tabulate(x, nlevels(x)) & (!match(levels(x), exclude, 0L)))
ans = match(as.integer(x), lev)
setattr(ans, 'levels', levels(x)[lev])
Expand Down
6 changes: 4 additions & 2 deletions R/fmelt.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ measurev = function(fun.list, sep="_", pattern, cols, multiple.keyword="value.na
other.values = lapply(group.dt[, is.other, with=FALSE], unique)
other.values$stringsAsFactors = FALSE
other.dt = data.table(do.call(expand.grid, other.values))
measure.list = structure(list(), variable_table=other.dt)
measure.list = list()
attr(measure.list, "variable_table") = other.dt
column.values = unique(group.dt[[multiple.keyword]])
for (column.val in column.values) {
select.dt = data.table(other.dt)
Expand All @@ -174,7 +175,8 @@ measurev = function(fun.list, sep="_", pattern, cols, multiple.keyword="value.na
}
measure.list
} else {# single output column.
structure(measure.vec, variable_table=group.dt)
attr(measure.vec, "variable_table") = group.dt
measure.vec
}
}

Expand Down
5 changes: 4 additions & 1 deletion inst/tests/S4.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ removeClass("CustomDurationClass")

# data.table(s4) #6874 should work
s4cl = setClass("s4cl", slots=list(x="integer"))
DT = setalloccol(structure(list(a=new("s4cl", x=1L)), row.names=c(NA, -1L), class=c("data.table", "data.frame")))
DT = list(a=new("s4cl", x=1L))
attr(DT, "row.names") = c(NA, -1L)
class(DT) = c("data.table", "data.frame")
setalloccol(DT)
test(9, data.table(a=s4cl(x=1L)), DT)
removeClass("s4cl")
36 changes: 18 additions & 18 deletions inst/tests/nafill.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ test(1.41, nafill(l, "locf"), lapply(l, nafill, "locf"))
test(1.42, nafill(l, "nocb"), lapply(l, nafill, "nocb"))
test(1.43, nafill(l, fill=0), lapply(l, nafill, fill=0))
l = list(a=c(1:2,NA,4:5), b=as.Date(c(1:2,NA,4:5), origin="1970-01-01"), d=c(NA,2L,NA,4L,NA), e=as.Date(c(NA,2L,NA,4L,NA), origin="1970-01-01")) # Date retain class #3617
test(1.44, nafill(l, "locf"), list(a=c(1:2,2L,4:5), b=structure(c(1,2,2,4,5), class="Date"), d=c(NA,2L,2L,4L,4L), e=structure(c(NA,2,2,4,4), class="Date")))
test(1.45, nafill(l, "nocb"), list(a=c(1:2,4L,4:5), b=structure(c(1,2,4,4,5), class="Date"), d=c(2L,2L,4L,4L,NA), e=structure(c(2,2,4,4,NA), class="Date")))
test(1.46, nafill(l, fill=0), list(a=c(1:2,0L,4:5), b=structure(c(1,2,0,4,5), class="Date"), d=c(0L,2L,0L,4L,0L), e=structure(c(0,2,0,4,0), class="Date")))
test(1.47, nafill(l, fill=as.Date(0, origin="1970-01-01")), list(a=c(1:2,0L,4:5), b=structure(c(1,2,0,4,5), class="Date"), d=c(0L,2L,0L,4L,0L), e=structure(c(0,2,0,4,0), class="Date")))
test(1.48, nafill(l, fill=as.Date("2019-06-05")), list(a=c(1:2,18052L,4:5), b=structure(c(1,2,18052,4,5), class="Date"), d=c(18052L,2L,18052L,4L,18052L), e=structure(c(18052,2,18052,4,18052), class="Date")))
test(1.44, nafill(l, "locf"), list(a=c(1:2,2L,4:5), b=.Date(c(1,2,2,4,5)), d=c(NA,2L,2L,4L,4L), e=.Date(c(NA,2,2,4,4))))
test(1.45, nafill(l, "nocb"), list(a=c(1:2,4L,4:5), b=.Date(c(1,2,4,4,5)), d=c(2L,2L,4L,4L,NA), e=.Date(c(2,2,4,4,NA))))
test(1.46, nafill(l, fill=0), list(a=c(1:2,0L,4:5), b=.Date(c(1,2,0,4,5)), d=c(0L,2L,0L,4L,0L), e=.Date(c(0,2,0,4,0))))
test(1.47, nafill(l, fill=as.Date(0, origin="1970-01-01")), list(a=c(1:2,0L,4:5), b=.Date(c(1,2,0,4,5)), d=c(0L,2L,0L,4L,0L), e=.Date(c(0,2,0,4,0))))
test(1.48, nafill(l, fill=as.Date("2019-06-05")), list(a=c(1:2,18052L,4:5), b=.Date(c(1,2,18052,4,5)), d=c(18052L,2L,18052L,4L,18052L), e=.Date(c(18052,2,18052,4,18052))))
test(1.49, nafill(numeric()), numeric())
if (test_bit64) {
l = list(a=as.integer64(c(1:2,NA,4:5)), b=as.integer64(c(NA,2L,NA,4L,NA)))
Expand Down Expand Up @@ -105,7 +105,7 @@ dt = copy(db)
test(2.07, {setnafill(dt, "locf", cols=c("V2","V3")); dt}, db[, c(list(V1), nafill(.SD, "locf"), list(V4)), .SDcols=c("V2","V3")])
l = list(a=c(1:2,NA,4:5), b=as.Date(c(1:2,NA,4:5), origin="1970-01-01"), d=c(NA,2L,NA,4L,NA), e=as.Date(c(NA,2L,NA,4L,NA), origin="1970-01-01")) # Date retain class #3617
setnafill(l, fill=as.Date("2019-06-05"))
test(2.08, unname(l), list(c(1:2,18052L,4:5), structure(c(1,2,18052,4,5), class="Date"), c(18052L,2L,18052L,4L,18052L), structure(c(18052,2,18052,4,18052), class="Date")))
test(2.08, unname(l), list(c(1:2,18052L,4:5), .Date(c(1,2,18052,4,5)), c(18052L,2L,18052L,4L,18052L), .Date(c(18052,2,18052,4,18052))))

# exceptions test coverage
x = 1:10
Expand Down Expand Up @@ -277,18 +277,18 @@ local({
test(10.18, coerceAs(c(1,2,3), xy_factor), output="double[numeric] into integer[factor]", error="factor numbers.*3.000000 is outside the level range")
test(10.19, coerceAs(factor("x"), xy_factor), factor("x", levels=c("x","y")), output="integer[factor] into integer[factor]")
test(10.20, coerceAs(factor("x"), xy_factor, copy=FALSE), factor("x", levels=c("x","y")), output="input already of expected type and class") ## copy=F has copyMostAttrib
a = structure("a", class="a")
b = structure("b", class="b")
test(10.21, coerceAs(a, b), structure("a", class="b"), output="character[a] into character[b]")
a = structure(1L, class="a")
b = structure(2L, class="b")
test(10.22, coerceAs(a, b), structure(1L, class="b"), output="integer[a] into integer[b]")
a = structure(1, class="a")
b = structure(2, class="b")
test(10.23, coerceAs(a, b), structure(1, class="b"), output="double[a] into double[b]")
a = structure(1, class="a")
b = structure(2L, class="b")
test(10.24, coerceAs(a, b), structure(1L, class="b"), output="double[a] into integer[b]")
a = `class<-`("a", "a")
b = `class<-`("b", "b")
test(10.21, coerceAs(a, b), `class<-`("a", "b"), output="character[a] into character[b]")
a = `class<-`(1L, "a")
b = `class<-`(2L, "b")
test(10.22, coerceAs(a, b), `class<-`(1L, "b"), output="integer[a] into integer[b]")
a = `class<-`(1, "a")
b = `class<-`(2, "b")
test(10.23, coerceAs(a, b), `class<-`(1, "b"), output="double[a] into double[b]")
a = `class<-`(1, "a")
b = `class<-`(2L, "b")
test(10.24, coerceAs(a, b), `class<-`(1L, "b"), output="double[a] into integer[b]")
if (test_bit64) {
x = as.integer64(1L)
test(10.81, coerceAs(x, 1), 1, output="double[integer64] into double[numeric]")
Expand Down
15 changes: 10 additions & 5 deletions inst/tests/optimize.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,19 @@ test(2231.63,optimize=opt, DT[, weighted.mean(x, w, na.rm=FALSE), g], DT[, st
test(2231.64,optimize=opt, DT[, weighted.mean(x, weight=w, na.rm=TRUE)], DT[, stats::weighted.mean(x, weight=w, na.rm=TRUE)])

# GForce retains attributes in by arguments #5567
dt = data.table(a=letters[1:4], b=structure(1:4, class = c("class_b", "integer"), att=1), c=structure(c(1L,2L,1L,2L), class = c("class_c", "integer")))
opt = c(0,Inf)
b = 1:4
class(b) = c("class_b", "integer")
attr(b, "att") = 1
cc = rep(1:2, 2L)
class(cc) = c("class_c", "integer")
DT = data.table(a=letters[1:4], b=b, c=cc)
opt = c(0, Inf)
out = c("GForce FALSE", "GForce optimized j to")
test(2263.1,optimize=opt, options=list(datatable.verbose=TRUE), dt[, .N, b], data.table(b=dt$b, N=1L), output=out)
test(2263.1,optimize=opt, options=list(datatable.verbose=TRUE), DT[, .N, b], data.table(b=DT$b, N=1L), output=out)
# test 2263.1 subsumes 2263.1 and 2263.4 for different optimization levels
test(2263.2,optimize=opt, options=list(datatable.verbose=TRUE), dt[, .N, .(b,c)], data.table(b=dt$b, c=dt$c, N=1L), output=out)
test(2263.2,optimize=opt, options=list(datatable.verbose=TRUE), DT[, .N, .(b,c)], data.table(b=DT$b, c=DT$c, N=1L), output=out)
# test 2263.2 subsumes 2263.2 and 2263.5 for different optimization levels
test(2263.3,optimize=opt, options=list(datatable.verbose=TRUE), names(attributes(dt[, .N, b]$b)), c("class", "att"), output=out)
test(2263.3,optimize=opt, options=list(datatable.verbose=TRUE), names(attributes(DT[, .N, b]$b)), c("class", "att"), output=out)
# test 2263.3 subsumes 2263.3 and 2263.6 for different optimization levels

# named arguments of c() in j get prepended to lapply(.SD, FUN) #2311
Expand Down
57 changes: 20 additions & 37 deletions inst/tests/other.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ if (FALSE) { # loaded[["reshape"]]
# The bug was that names(DT) changed, hence testing DT here not ans. Same fix tested next with caret, so we now just rely on the caret test.
# When running this test on 13 Mar 2018, I noticed that reshape::cast doesn't retain the Date class and returns just numbers. So I copied
# this test to the reshape2 section in main tests.Rraw, changed it to use dcast instead and tested the result explicitly.
DT = data.table(ID = c(611557L, 611557L, 611557L, 894125L, 894125L, 894125L, 894125L, 894125L, 898856L, 898856L, 898856L, 898856L, 898856L, 898856L, 898899L, 898899L, 898899L), DATUM = structure(c(16101, 16071, 16261, 16104, 16133, 16167, 16201, 16236, 16089, 16118, 16147, 16176, 16236, 16208, 16163, 16125, 16209), class = "Date"), N = c(25L, 9L, 23L, 29L, 26L, 26L, 27L, 28L, 39L, 39L, 38L, 36L, 40L, 39L, 19L, 20L, 19L), rank = c(2, 1, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 6, 5, 2, 1, 3))
DT = data.table(ID = c(611557L, 611557L, 611557L, 894125L, 894125L, 894125L, 894125L, 894125L, 898856L, 898856L, 898856L, 898856L, 898856L, 898856L, 898899L, 898899L, 898899L), DATUM = .Date(c(16101, 16071, 16261, 16104, 16133, 16167, 16201, 16236, 16089, 16118, 16147, 16176, 16236, 16208, 16163, 16125, 16209)), N = c(25L, 9L, 23L, 29L, 26L, 26L, 27L, 28L, 39L, 39L, 38L, 36L, 40L, 39L, 19L, 20L, 19L), rank = c(2, 1, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 6, 5, 2, 1, 3))
ans = cast(DT, ID ~ rank, value = "DATUM")
test(3, names(DT), c("ID", "DATUM", "N", "rank"))
}
Expand Down Expand Up @@ -566,52 +566,35 @@ df = data.frame(a=1:2, b=3:2)
dt = as.data.table(df)
mx = matrix(1:9, 3, 3)
ar = array(1:27, c(3,3,3))
xt = structure(
xt = matrix(
c(142.25, 141.229996, 141.330002, 142.860001, 142.050003, 141.399994,
140.570007, 140.610001, 140.380005, 141.369995, 141.669998, 140.539993,
94807600, 69620600, 76645300, 108.999954, 109.231255, 108.360008),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(c(1167782400, 1167868800, 1167955200), tzone = "UTC", tclass = "Date"),
dim = c(3L, 6L), dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
nrow=3L, ncol=6L,
dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
)
class(xt) = c("xts", "zoo")
attr(xt, "index") = c(1167782400, 1167868800, 1167955200)
attr(xt, ".indexCLASS") = attr(xt, "tclass") = attr(attr(xt, "index"), "tclass") = "Date"
attr(xt, ".indexTZ") = attr(xt, "tzone") = attr(attr(xt, "index"), "tzone") = "UTC"
local({
old = options(datatable.verbose=TRUE); on.exit(options(old))
if (loaded[["xts"]]) {
test(19.21, last(x, n=2L), 2:3, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.22, last(y, n=2L), y[2:3], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.23, last(x, n=1L), 3L, output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.24, last(y, n=1L), y[3L], output="using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
xt_last = structure(
c(141.330002, 141.399994, 140.380005, 140.539993, 76645300, 108.360008),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(1167955200, tzone = "UTC", tclass = "Date"),
dim = c(1L, 6L), dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
)
xt_last2 = structure(
c(141.229996, 141.330002, 142.050003, 141.399994, 140.610001, 140.380005,
141.669998, 140.539993, 69620600, 76645300, 109.231255, 108.360008),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(c(1167868800, 1167955200), tzone = "UTC", tclass = "Date"),
dim = c(2L, 6L), dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
)
# NB: since xts is loaded, `[` dispatch will work
xt_last = xt[3L, ]
xt_last2 = xt[2:3, ]
test(19.25, last(xt), xt_last, output="using xts::last: is.xts(x)")
test(19.26, last(xt, n=2L), xt_last2, output="using xts::last: is.xts(x)")
test(19.31, first(x, n=2L), 1:2, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.32, first(y, n=2L), y[1:2], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.33, first(x, n=1L), 1L, output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
test(19.34, first(y, n=1L), y[1L], output="using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()")
xt_first = structure(
c(142.25, 142.860001, 140.570007, 141.369995, 94807600, 108.999954),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(1167782400, tzone = "UTC", tclass = "Date"),
dim = c(1L, 6L), dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
)
xt_first2 = structure(
c(142.25, 141.229996, 142.860001, 142.050003, 140.570007, 140.610001, 141.369995, 141.669998, 94807600, 69620600, 108.999954, 109.231255),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
index = structure(c(1167782400, 1167868800), tzone = "UTC", tclass = "Date"),
dim = c(2L, 6L), dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"))
)
xt_first = xt[1L, ]
xt_first2 = xt[1:2, ]
test(19.35, first(xt), xt_first, output="using xts::first: is.xts(x)")
test(19.36, first(xt, n=2L), xt_first2, output="using xts::first: is.xts(x)")
} else {
Expand All @@ -632,22 +615,22 @@ local({
test(19.42, last(y), y[3L], output="using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))")
test(19.51, first(x), 1L, output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))")
test(19.52, first(y), y[1L], output="using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))")
test(19.61, last(df), structure(list(a=2L, b=2L), row.names=2L, class="data.frame"), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
test(19.61, last(df), data.frame(a=2L, b=2L, row.names=2L), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
test(19.62, last(dt), data.table(a=2L, b=2L), output="using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
test(19.71, first(df), structure(list(a=1L, b=3L), row.names=1L, class="data.frame"), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
test(19.71, first(df), data.frame(a=1L, b=3L, row.names=1L), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
test(19.72, first(dt), data.table(a=1L, b=3L), output="using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)")
# matrix/array utils::tail behavior is likely to change in future R, Michael is more in the topic
test(19.81, last(mx), structure(c(3L, 6L, 9L), dim = c(1L, 3L), dimnames = list("[3,]", NULL)), output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
expected = if (base::getRversion() < "3.7.0") 27L else structure(c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 24L, 27L), dim = c(1L, 3L, 3L), dimnames = list("[3,]", NULL, NULL)) #4127
test(19.81, last(mx), matrix(c(3L, 6L, 9L), ncol=3L, dimnames = list("[3,]", NULL)), output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
expected = if (base::getRversion() < "3.7.0") 27L else array(c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 24L, 27L), dim = c(1L, 3L, 3L), dimnames = list("[3,]", NULL, NULL)) #4127
test(19.82, last(ar), expected, output="using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
test(19.91, first(mx), structure(c(1L, 4L, 7L), dim = c(1L, 3L)), output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
expected = if (base::getRversion() < "3.7.0") 1L else structure(c(1L, 4L, 7L, 10L, 13L, 16L, 19L, 22L, 25L), dim = c(1L, 3L, 3L)) #4127
test(19.91, first(mx), matrix(c(1L, 4L, 7L), ncol=3L), output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
expected = if (base::getRversion() < "3.7.0") 1L else array(c(1L, 4L, 7L, 10L, 13L, 16L, 19L, 22L, 25L), dim = c(1L, 3L, 3L)) #4127
test(19.92, first(ar), expected, output="using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)")
})

if (loaded[["xts"]]) { # was 2133 in tests.Rraw, #5516
# keep.rownames in as.data.table.xts() supports a string, #4232
xts = xts::xts(1:10, structure(1:10, class = "Date"))
xts = xts::xts(1:10, .Date(1:10))
colnames(xts) = "VALUE"
DT = as.data.table(xts, keep.rownames = "DATE", key = "DATE")
test(20.1, colnames(DT), c("DATE", "VALUE"))
Expand Down
Loading
Loading