I am a new user of the data.table package in R. I am trying to give a name to the new column created by a "group by" command
> DT = data.table(x=rep(c("a","b"),c(2,3)),y=1:5)
> DT
x y
1: a 1
2: a 2
3: b 3
4: b 4
5: b 5
> DT[,{z=sum(y);z+3},by=x]
x V1
1: a 6
2: b 15
Additionally, is it possible to perform several group by operations in one command, that would result in something like:
x V1 V2
1: a 6 something
2: b 15 something
Thanks
data. table(DT) is TRUE. To better description, I put parts of my original code here. So you may understand where goes wrong.
1 Answer. The explanation: colnames() is the function to set column names for a matrix. rownames() is the function to set row names for a matrix.
Columns in the same table or view cannot have the same name. However, columns in different tables or views can have the same name.
DT[,list(z=sum(y)+3,a=mean(y*z)),by=x]
x z a
1: a 6 9
2: b 15 60
Since you are new to data.table
, I recommend that you also study the help page of the setnames
function as well as ?data.table
and the data.table
vignettes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With