I am trying to make a section of code more flexible by referencing the data frame column names and inserting them into an equation, rather than calling the names directly. The following example works, although I have to insert the field name directly:
require(e1071)
class = c(0.25, 0.34, 0.55)
field1 = c(23, 33, 34)
field2 = c(44, 55, 32)
df = data.frame(class, field1, field2)
mysvm = svm(class ~ field1 + field2, data = df)
The following example does not work, and I do not know why:
require(e1071)
class = c(0.25, 0.34, 0.55)
field1 = c(23, 33, 34)
field2 = c(44, 55, 32)
df = data.frame(class, field1, field2)
name1 = names(df)[2]
name2 = names(df)[3]
mysvm = svm(class ~ name1 + name2, data = df)
How can I reference the 2nd and 3rd columns in a dataframe and properly insert them into an equation?