sds - 1 year ago 91

R Question

Suppose I have a

`data.table`

`a <- data.table(id=1:1000, x=runif(100), y=runif(100), z=runif(100))`

I want to drop the rows where

`x`

`y`

`z`

`a <- a[ x > median(x) & y > median(y) & z > median(z) ]`

(aside: does the above call

`median`

What I do is

`my.cols <- c("x","y","z")`

my.meds <- sapply(my.cols, function(n) median(a[[n]]))

a <- a[ Reduce(`&`,Map(function(i) a[[my.cols[i]]] > my.meds[i], 1:length(my.cols))) ]

Is this the best I could do?

Answer Source

One option is to construct the string you want and `eval/parse`

it:

```
EVAL = function(...)eval(parse(text=paste0(...))) # standard helper function
a[ EVAL(my.cols, ">median(", my.cols, ")", collapse=" & ") ]
```