I want to create a column in a data frame in which each row is the solution to an equation with 1 unknown (x
). The other variables in the equation are provided in the other columns. In another Stack Overflow question, @flodel provided a solution, which I have tried to adapt. However, the output data frame omits some observations entirely, and others have "duplicates" with two different solutions to the same equation.
Sample of my data frame:
Time | id | V1 | V2 | V3 | V4 |
---|---|---|---|---|---|
199304 | 79330 | 259.721 | 224.5090 | 0.040140442 | 0.08100474 |
201004 | 77520 | 5062.200 | 3245.6921 | 0.037812662 | 0.08509553 |
196804 | 23018 | 202.897 | 842.6852 | 0.154956206 | 0.12982818 |
197804 | 12319 | 181.430 | 341.4415 | 0.052389156 | 0.14196588 |
199404 | 18542 | 14807.000 | 16537.0873 | -0.001394388 | 0.08758791 |
Code with the equation I want to solve. I have simplified the equation, but the issue relates to this simple equation too.
library(plyr)
library(rootSolve
set.seed(1)
df <- adply(df, 1, summarize,
x = uniroot.all(function(x) V1 * ((V4-V3)/(x-V3)) - V2,
interval = c(-10,10)))
How can I achieve this? If possible, it would be great to do this in an efficient manner, as my actual data frame has >1,000,000 rows