Exercise

# Mixing it together: lapply, .SD, .SDcols and .N

This exercise is a challenging one, so don't give up! It's important to remember that whenever the `j`

argument is a list (e.g. if it contains `.SD`

or a call to `lapply()`

), a data.table is returned. For example:

```
dogs[, lapply(.SD, mean), by = sex, .SDcols = c("weight", "age")]
```

will return a data.table containing average weights and ages for dogs of each sex.

It's also helpful to know that combining a list with a vector results in a new longer list. Lastly, note that when you select `.N`

on its own, it is renamed `N`

in the output for convenience when chaining.

For this exercise, `DT`

, which contains variables `x`

, `y`

, and `z`

, is loaded in your workspace. You must combine `lapply()`

, `.SD`

, `.SDcols`

, and `.N`

to get your call to return a specific output. Good luck!

Instructions

**100 XP**

Get the sum of all columns

`x`

,`y`

and`z`

and the number of rows in each group while grouping by`x`

. Your answer should be identical to this:`x x y z N 1: 2 8 26 30 4 2: 1 3 23 26 3`

Get the cumulative sum of column

`x`

and`y`

while grouping by`x`

and`z > 8`

such that the answer looks like this:`by1 by2 x y 1: 2 FALSE 2 1 2: 2 FALSE 4 6 3: 1 FALSE 1 3 4: 1 FALSE 2 10 5: 2 TRUE 2 9 6: 2 TRUE 4 20 7: 1 TRUE 1 13`