I have the following dataset:
my.df <- data.frame(my_function=rep(c("Var1+Var 2","Var 2-Var1","(Var 2-(Var 2-Var1))/Var 2"), 1),
`Var1`=rep(1:1,3),
`Var 2`=rep(5:5,3), check.names = FALSE)
my.df
# my_function Var1 Var 2
# 1 Var1+Var 2 1 5
# 2 Var 2-Var1 1 5
# 3 (Var 2-(Var 2-Var1))/Var 2 1 5
And I want to use column named my_function
to calculate the values for each row into a new column called outcome
The outcome
would be: 1+5=6
,5-1=4
,(5-(5-1))/5=0.2
for each of the rows.
EDIT Correct answers also reference the following original dataset:
my.df <- data.frame(my_function=rep(c("1000+2000","2000-1000","(2000-(2000-1000))/2000"), 1), `1000`=rep(1:1,3), `2000`=rep(5:5,3))