r - Apply a function on multiple columns in each row -
below data frame have. column 2 days expiration of nearest contract, column 3 days expiration of next nearest contract. i'm trying create vector gives me percentage of column 2 needed give me weighted average days expiration of 28 days each row.
date daysxone daysxtwo 1 2006-01-03 15 43 days 2 2006-01-04 14 42 days 3 2006-01-05 13 41 days 4 2006-01-06 12 40 days 5 2006-01-09 9 37 days 6 2006-01-10 8 36 days
i've tried:
f <- function(x){ df$daysxone*(x) + (df$daysxtwo*(1-(x)) -28}
and i've tried few things uniroot(), i'm stuck
thanks!
or go data.table
:
library(data.table) df <- data.frame(daysxone = c(15,14,13,12,9,8), daysxtwo = c(43, 42,41,40, 37,36)) setdt(df)[,perc := (28-daysxtwo)/(daysxone - daysxtwo),] daysxone daysxtwo perc 1: 15 43 0.5357143 2: 14 42 0.5000000 3: 13 41 0.4642857 4: 12 40 0.4285714 5: 9 37 0.3214286 6: 8 36 0.2857143
Comments
Post a Comment