Easiest to show an example for my question. I want to set the column name to something other than N in the code below:
dt <- data.table(
ID = c("b","b","b","a","a","c"),
x = 1:6,
y = 7:12,
z = 13:18
)
cols = c("x", "y")
dt[, c(lapply(.SD, mean),.N), .SDcols=cols, keyby = c("ID")]
# returns
ID x y N
1: a 4.5 10.5 2
2: b 2.0 8.0 3
3: c 6.0 12.0 1
I would like to set the name of the N column in the original call (my actual use case will involve more columns and functions) so that the output is
ID x y Members
1: a 4.5 10.5 2
2: b 2.0 8.0 3
3: c 6.0 12.0 1
I could of course rename N in a separate command after, but I am curious if I can do it as part of the groupby line.
# attempt 1 which still returns column named N
dt[, c(lapply(.SD, mean), Members = .N), .SDcols=cols, keyby = c("ID")]
>
ID x y N
1: a 4.5 10.5 2
2: b 2.0 8.0 3
3: c 6.0 12.0 1
# attempt 2 errors
dt[, (c(cols, "Members")) = c(lapply(.SD, mean), .N), .SDcols=cols, keyby = c("ID")]
Error in parse(text = .DATABRICKS_CURRENT_TEMP_CMD__) :
<text>:10:27: unexpected '='
9: cols = c("x", "y")
10: dt[, (c(cols, "Members")) =
^
Error in parse(text = .DATABRICKS_CURRENT_TEMP_CMD__): <text>:10:27: unexpected '='
Error in parse(text = .DATABRICKS_CURRENT_TEMP_CMD__): <text>:10:27: unexpected '='
9: cols = c("x", "y")
10: dt[, (c(cols, "Members")) =
Versions: data.table 1.14.8 and R 4.3.1