I have a data.frame df with 2 columns in R and I need to update one of the columns by using a eval
function (or by any other way) based on the values in the other column. Col1
is a character column and I need to evaluate the expression and update the result in Col2
.
So, for the first row it would be -0.49, for the last one: -0.26.
I tried this approach but it updates all the values with the result from last row's evaluation:
df["Col2"] <- eval(parse(text=df$Col1))
Col1 Col2
------------------------------
20.31 - 20.80 0
51.81 - 52.22 0
44.07 - 44.88 0
42.94 - 43.47 0
18.93 - 19.15 0
27.42 - 27.68 0
I got this unexpected result:
Col1 Col2
------------------------------
20.31 - 20.80 -0.26
51.81 - 52.22 -0.26
44.07 - 44.88 -0.26
42.94 - 43.47 -0.26
18.93 - 19.15 -0.26
27.42 - 27.68 -0.26
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…