pastadisaster
pastadisaster

Reputation: 77

How to subset a data table by one column with minimal value in another

Say I have a process dependent on two inputs with one output. The process has already been numerically simulated elsewhere and it is not practical to run these simulations again.

I have stored the data in a table. The table contains (amongst other things) the two key input parameters and the resulting output parameter (here v,q and quant respectively).

> v=0.01*rep(1:100, each=100)
> q=0.01*rep(seq(1:100),100)
> quant <- rnorm(10000, mean=0.5, sd=0.1)
> fd <- data.table(q,v, quant)

My question: The input parameter space is subdivided by discrete values of one of the inputs (here, v), and I want to know how to extract a subset of my table where the value of the second input (q) yields some extreme value of the output (say closest to a particular value alpha such that we seek min(abs(quant-alpha))) within the subset of the other input remaining constant. for example, assume alpha=0.5

fd1 <- subset(fd,???min(abs(quant-0.5)),by=v)

So the resulting table will have unique values of v and values of quant which satisfy min(abs(quant-alpha)) for specified alpha and v. The table must also contain the relevant value of q and any other data contained in the row.

I believe that there should be a very simple solution to this question, and that I am simply too novice to know how to find it!

Upvotes: 1

Views: 387

Answers (1)

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59970

Are you just trying to do this....

fd[  , .SD[ which.min( abs(quant-0.5) ) ] , by = v ]
#        v    q     quant
#  1: 0.01 0.42 0.5010319
#  2: 0.02 0.71 0.4983129
#  3: 0.03 0.47 0.4996793
#  4: 0.04 0.01 0.5028813
#  5: 0.05 0.93 0.5009666
#  6: 0.06 0.93 0.4996367

You get the row for each group ( by = v ) that satisfies the condition using which.min to subset .SD.

Upvotes: 2

Related Questions