What is the correct syntax for this data.table task in R?

I have data.table in R :

  library(data.table) DT = data.table(x=rep(c("b","a","c"),each=3), y=sample(rnorm(9)), v=1:9) 

I just want to calculate the minimum and maximum for column x and add these two new columns to DT . Here is my line for this:

  DT[,c("e","d"):= list(min(y),max(y)), with=FALSE, by = x] Error in `[.data.table`(DT, , `:=`(c("e", "d"), list(min(y), max(y))), : 'with' must be TRUE when 'by' or 'keyby' is provided 

However, if I write: DT[,c("e","d"):= list(min(y),max(y)), with=FALSE] , I get the following:

  xyved 1: a -1.7125000 4 -1.7125 1.30553 2: a 1.0198038 5 -1.7125 1.30553 3: a 1.3055301 6 -1.7125 1.30553 4: b -0.9238759 1 -1.7125 1.30553 5: b 0.3077016 2 -1.7125 1.30553 6: b -1.2580845 3 -1.7125 1.30553 7: c -0.9399120 7 -1.7125 1.30553 8: c -0.1910583 8 -1.7125 1.30553 9: c 0.1239158 9 -1.7125 1.30553 

As you can see, this works, but does not complete the task with x . I want to get something similar, but e and d must be computed by each value of the variable x . So my question is: how can I solve this?

+4
source share
2 answers

" := by group" (new in version 1.8.2) and " := with several new columns" (new in version 1.7.8) are relatively recent additions to data.table .

" := for groups with several new columns" is simply not yet implemented.

So now you can do it (if you want a single liner):

 setkey(DT, "x") DT <- DT[DT[,list(e=min(y), d=max(y)), by=key(DT)]] 

or this (if you want to minimize additional copy operations):

 setkey(DT, "x") DT[,e:=min(y), by=key(DT)] DT[,d:=max(y), by=key(DT)] 
+4
source

This is the syntax. DT must have x as the key to work.

 DT = data.table(x=rep(c("b","a","c"),each=3), y=sample(rnorm(9)), v=1:9) setkey(DT, x) DT[DT[, list(e=min(y), d=max(y)), by=x]] xyved 1: a 0.04583602 4 -0.08423764 0.5778324 2: a 0.57783240 5 -0.08423764 0.5778324 3: a -0.08423764 6 -0.08423764 0.5778324 4: b -1.30934873 1 -1.30934873 0.3071819 5: b -0.05972203 2 -1.30934873 0.3071819 6: b 0.30718188 3 -1.30934873 0.3071819 7: c -0.72347616 7 -0.72347616 0.7800817 8: c -0.46930825 8 -0.72347616 0.7800817 9: c 0.78008168 9 -0.72347616 0.7800817 
+3
source

Source: https://habr.com/ru/post/1433428/


All Articles