Skip to content

Commit c27a231

Browse files
authored
dcast instead of SDcols (#3475)
using dcast is simpler so you may consider mentioning that, for example below ```r > data.table(iris)[, unlist(lapply(.SD, function(x) c(max=max(x), min=min(x)))), .SDcols = c("Petal.Length", "Petal.Width") ] Petal.Length.max Petal.Length.min Petal.Width.max Petal.Width.min 6.9 1.0 2.5 0.1 > dcast(data.table(iris), . ~ ., list(max,min), value.var = c("Petal.Length", "Petal.Width")) Key: <.> . Petal.Length_max Petal.Width_max Petal.Length_min Petal.Width_min <char> <num> <num> <num> <num> 1: . 6.9 2.5 1 0.1 ```
1 parent 9e26a13 commit c27a231

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/src/man/comparisons.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ df2 <- data.table(grp=c(1,3), w = c(10,11))
285285
| Transform several columns | `df[, .(max(x), min(y)) ]` | `combine(df, :x => maximum, :y => minimum)` |
286286
| | `df[, lapply(.SD, mean), .SDcols = c("x", "y") ]` | `combine(df, [:x, :y] .=> mean)` |
287287
| | `df[, lapply(.SD, mean), .SDcols = patterns("*x") ]` | `combine(df, names(df, r"^x") .=> mean)` |
288-
| | `df[, unlist(lapply(.SD, function(x) c(max=max(x), min=min(x)))), .SDcols = c("x", "y") ]` | `combine(df, ([:x, :y] .=> [maximum minimum])...)` |
288+
| | `dcast(df, . ~ ., list(max,min), value.var = c("x","y"))` | `combine(df, ([:x, :y] .=> [maximum minimum])...)` |
289289
| Multivariate function | `df[, .(cor(x,y)) ]` | `transform(df, [:x, :y] => cor)` |
290290
| Row-wise | `df[, min_xy := min(x, y), by = 1:nrow(df)]` | `transform!(df, [:x, :y] => ByRow(min))` |
291291
| | `df[, argmax_xy := which.max(.SD) , .SDcols = patterns("*x"), by = 1:nrow(df) ]` | `transform!(df, AsTable(r"^x") => ByRow(argmax))` |

0 commit comments

Comments
 (0)