Simplify notation in math operations within dataframe

Viewed 30

Say I have a dataframe data with 5 variables, var1,var2,var3,var4,var5. Imagine I want to do the following operation:

sqrt(data$var2)*(data$var4+data$var1)/data$var3 + data$var5/log(data$var3)

Is there a simpler way to just call the formula as follows?

sqrt(var2)*(var4+var1)/var3 + var5/log(var3)

Perhaps within another function? apply or the like? Can't get how to do it properly. Removing variables from the dataframe is not a desirable option.

1 Answers

Just wrap using with

with(data, sqrt(var2)*(var4+var1)/var3 + var5/log(var3))

Or another option is transform

transform(data, new = sqrt(var2)*(var4+var1)/var3 + var5/log(var3)))

Another option which is undesirable is creating column names as objects in the global env with attach. Then, can use the objects directly. But, it is not recommended because this will pollute the env with lots of objects and can have some side effects

attach(data)
sqrt(var2)*(var4+var1)/var3 + var5/log(var3)
Related