R: data.table. Dynamic Aggregations of Column Column Columns

I am trying to make an aggregate min/ maxon a dynamically selected column in data.table. It works fine for columns numeric, but I can't get it to work with columns Dateunless I create a temporary one data.table.

It works when I use the name:

dt <- data.table(Index=1:31, Date = seq(as.Date('2015-01-01'), as.Date('2015-01-31'), by='days'))
dt[, .(minValue = min(Date), maxValue = max(Date))]
# minValue   maxValue
# 1: 2015-01-01 2015-01-31

This does not work when I use with=FALSE:

colName = 'Date'
dt[, .(minValue = min(colName), maxValue = max(colName)), with=F]
# Error in `[.data.table`(dt, , .(minValue = min(colName), maxValue = max(colName)),  : 
# could not find function "."

I can use .SDcolsin a numeric column:

colName = 'Index'
dt[, .(minValue = min(.SD), maxValue = max(.SD)), .SDcols=colName]
#   minValue maxValue
#  1:        1       31

But I get an error when I do the same for the Date column:

colName = 'Date'
dt[, .(minValue = min(.SD), maxValue = max(.SD)), .SDcols=colName]
# Error in FUN(X[[i]], ...) : 
#   only defined on a data frame with all numeric variables

If I use lapply(.SD, min)or sapply(), the dates will be changed to numbers.

The following works and does not seem to lose memory and works quickly. Anything better?

a <- dt[, colName, with=F]
setnames(a, 'a')
a[, .(minValue = min(a), maxValue = max(a))]
+4
1

:

dt[, .(minValue = min(colName), maxValue = max(colName)), with=F]
# Error in `[.data.table`(dt, , .(minValue = min(colName), maxValue = max(colName)),  : 
# could not find function "."

data.table , , with=. , with() R.

:

dt[, .(minValue = min(.SD), maxValue = max(.SD)), .SDcols=colName]
# Error in FUN(X[[i]], ...) : 
#   only defined on a data frame with all numeric variables

min() max() data.frame/data.table . MRE.

df = data.frame(x=as.Date("2015-01-01"))
min(df)
# Error in FUN(X[[i]], ...) : 
#   only defined on a data frame with all numeric variables

, get():

dt[, .(min = min(get(colName)), max = max(get(colName)))]

, @Frank, [[ :

dt[, .(min = min(.SD[[colName]]), max = max(.SD[[colName]]))]

.SD ( R AFAICT, data.table R). FR # 1063. / , , :

# NOTE: not yet implemented, FR #1063
dt[, colwise(.SD, min, max), .SDcols = colName]
+2

Source: https://habr.com/ru/post/1614895/


All Articles