franknarf1 / r-tutorial

This book covers the essentials of using R
Creative Commons Zero v1.0 Universal
12 stars 4 forks source link

extend "defining data.table calls" #11

Open franknarf1 opened 7 years ago

franknarf1 commented 7 years ago

It should also illustrate how to name columns from j (instead of just getting V1):

Henrik's example: https://chat.stackoverflow.com/transcript/message/38440648#38440648

library(data.table)
d <- data.table(grp = c("a", "a", "a", "b", "b"), x = 1:5)

# one way
es = list()
es[["my_min"]] = quote(min(x))
es[["my_max"]] = quote(max(x))

e = as.call(c(as.symbol("list"), es))

# another way
e = setNames(
    quote(.(min(x), max(x))), 
    c("", "my_min", "my_max")
)

# then...
d[, eval(e), by=grp]

Josh's answer is helpful here: https://stackoverflow.com/a/14697835/

franknarf1 commented 5 years ago

Another example: https://stackoverflow.com/a/55733481

It might make sense to add exercises as well

jangorecki commented 5 years ago

Also this might be helpful https://stackoverflow.com/a/54800108/2490497