I have a multi column data frame and I want to build an svm model. The question is how can choose two specific columns as data. For instance if my data frame is like this:

df <- data.frame("x1"=c(1,2,3),"x2"=c(4,5,6),"x3"=c(7,8,9),"x4"=c(1,0,1))

I want to build an svm model with x2 and x3 with respect x4.

2 Answers

0
Lucca Ramalho On

You could use select from dplyr, like this:

df <- select(df, x1,x2,x3)
0
Prenil Sewmohan On

This should do the trick:

library(e1071)
df <- data.frame("x1"=c(1,2,3),"x2"=c(4,5,6),"x3"=c(7,8,9),"x4"=c(1,0,1))

#SVM model
model <- svm(x4 ~ x2 + x3, data = df)
summary(model)

#Model predictions
pred <- predict(model, subset(df,select=c(x2,x3)))
print(pred)

Specifically, I think the subset function is what you are looking for: subset(df,select=c(x2,x3))