theArun - 1 year ago 63

R Question

I am looking to find the "AND" or "OR" of all the elements by grouping based on column

`a`

`set.seed(1)`

dt <- data.table(a = sample(seq.int(1,100,1),100,replace =TRUE),b = sample(c(TRUE,FALSE),100,replace = TRUE))

# head(dt)

# a b

# 1: 27 FALSE

# 2: 38 TRUE

# 3: 58 TRUE

# 4: 91 FALSE

# 5: 21 FALSE

# 6: 90 TRUE

Answer Source

Perhaps we need

```
dt[, if(all(b)) .SD , by = a]
dt[, if(any(b)) .SD ,by = a]
```

If we need to extract the 'a' elements

```
dt[, if(all(b)) a, by = a]$V1
```