2

I'm trying to create a summarized data.table using the j column, but assign to a name stored in a variable.

For example, I can do this:

x = data.table(c(1,2,3,4,5,6),c(2,2,2,3,3,3))

x[,.("a" = mean(V1), "b" = max(V1)),by=V2]

which returns as wanted

   V2 a b
1:  2 2 3
2:  3 5 6

Now instead of using the name "a", I would like to use a variable name:

varname = "a"

x[,.(varname = mean(V1), "b" = max(V1)), by=V2]

I'd like it to return the same output, but of course here column a is labeled as "varname". I've tried using eval, get, and others and haven't figured out the right syntax. Is this built in, or will I have to relabel the name outside of data.table?

2 Answers 2

2

We can use setnames after the aggregation

out <- x[,.( mean(V1), "b" = max(V1)), by=V2]
setnames(out, 'V1', varname)
out
#   V2 a b
#1:  2 2 3
#2:  3 5 6

Or use setNames

x[, setNames(.(mean(V1), max(V1)), c(varname, "b")), by = V2]

With tidyverse, the lhs evaluation is possible

library(tidyverse)
x %>%
   group_by(V2) %>% 
   summarise(!! varname := mean(V1))
# A tibble: 2 x 2
#     V2     a
#   <dbl> <dbl>
#1     2     2
#2     3     5
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a reason the eval or get syntax doesn't work on the left hand side?
@AllenWang It is difficult to get that to work, but in tidyverse you can do x %>% group_by(V2) %>% summarise(!! varname := mean(V1))
1

Alternatively, you can put the functions into a named list as follows:

x[, lapply(structure(list(mean, max), names=c(varname, "b")), 
        function(f) f(V1)), 
    by=V2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.