using the same variable in aggregation and sorting in data table

Hi, I have a code

proxy <-"Var1"
       count   = .N
       ,summm  = sum(PREMIUM)      
     by = c(proxy,'var2')][order(Var2, proxy)]

How do I use the same proxy in "by" and "order"?

the "by" requires variable to come with quotation marks, and "order" requires a variable without quotation marks.

Treid the proxy <-noquote("Var1"), but data.table also complains, which is hard to understand why.


You've muddles a few things.

R is case-sensitive, so var2 and Var2 are different objects.

You can mix quoted and unquoted variables, but you need to be aware how they are used:


mt <- data.table(mtcars)

mt[, .N, by = c("cyl", "gear")] # use quotes when using a vector

mt[, .N, by = list(cyl, gear)] # don't use quotes when using a list

mt[, .N, .(cyl, gear)] # same but less typing

mt[, .N, keyby = .(cyl, gear)] # keyby keys (orders) the output

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.