How to process formula object to a character object?

Viewed 66

Here is my question:

formula = label ~ m1 + m2 + m2m1 + topix + sp500 + Open + High + Low +
  Close + Volume + psyco12 + psyco24 + closeB5 + closeB10 +
  closeB20 + closeB60 + William5 + William8 + William13 + William21 +
  William34 + RSV9 + K + D + KD + MA5 + MA10 + MA20 + MA60 +
  MA120 + MA240 + MA5MA20 + DI + EMA12 + EMA26 + DIF + MACD +
  OSC + change + RSI6p + RSI6m + RSI6 + RSI12p + RSI12m + RSI12 +
  RSI6RSI12 + OBV + JPYUSD + MarCap + MonthVol + MonthVal +
  GV

class(formula) is formula

i want to convert it to the following format.

c("m1","m2","m2m1","topix","sp500","Open","High","Low",
                 "Close","Volume","psyco12","psyco24","closeB5","closeB10", 
                 "closeB20","closeB60","William5","William8","William13","William21",
                 "William34","RSV9","K","D","KD","MA5","MA10","MA20","MA60",
                 "MA120","MA240","MA5MA20","DI","EMA12","EMA26","DIF","MACD",
                 "OSC","change","RSI6p","RSI6m","RSI6","RSI12p","RSI12m","RSI12", 
                 "RSI6RSI12","OBV","JPYUSD","MarCap","MonthVol","MonthVal","GV")

including that c()

How can I make this work?

2 Answers

An option would be reformulate to convert from vector of names to formula

f1 <- reformulate(v1, "label")

And for reverse, use the all.vars

v2 <- all.vars(f1)[-1]
dput(v2)
c("m1", "m2", "m2m1", "topix", "sp500", "Open", "High", "Low", 
"Close", "Volume", "psyco12", "psyco24", "closeB5", "closeB10", 
"closeB20", "closeB60", "William5", "William8", "William13", 
"William21", "William34", "RSV9", "K", "D", "KD", "MA5", "MA10", 
"MA20", "MA60", "MA120", "MA240", "MA5MA20", "DI", "EMA12", "EMA26", 
"DIF", "MACD", "OSC", "change", "RSI6p", "RSI6m", "RSI6", "RSI12p", 
"RSI12m", "RSI12", "RSI6RSI12", "OBV", "JPYUSD", "MarCap", "MonthVol", 
"MonthVal", "GV")

Or if there are multiple dependent variables,

all.vars(as.list(f1)[[3]])

data

v1 <- c("m1","m2","m2m1","topix","sp500","Open","High","Low",
                 "Close","Volume","psyco12","psyco24","closeB5","closeB10", 
                 "closeB20","closeB60","William5","William8","William13","William21",
                 "William34","RSV9","K","D","KD","MA5","MA10","MA20","MA60",
                 "MA120","MA240","MA5MA20","DI","EMA12","EMA26","DIF","MACD",
                 "OSC","change","RSI6p","RSI6m","RSI6","RSI12p","RSI12m","RSI12", 
                 "RSI6RSI12","OBV","JPYUSD","MarCap","MonthVol","MonthVal","GV")

Here is another option using @akrun's data (v1). Following the documentation on formula, you can get the variables using:

attr(terms.formula(formula), "term.labels")
# output
 [1] "m1"        "m2"        "m2m1"      "topix"     "sp500"    
 [6] "Open"      "High"      "Low"       "Close"     "Volume"   
[11] "psyco12"   "psyco24"   "closeB5"   "closeB10"  "closeB20" 
[16] "closeB60"  "William5"  "William8"  "William13" "William21"
[21] "William34" "RSV9"      "K"         "D"         "KD"       
[26] "MA5"       "MA10"      "MA20"      "MA60"      "MA120"    
[31] "MA240"     "MA5MA20"   "DI"        "EMA12"     "EMA26"    
[36] "DIF"       "MACD"      "OSC"       "change"    "RSI6p"    
[41] "RSI6m"     "RSI6"      "RSI12p"    "RSI12m"    "RSI12"    
[46] "RSI6RSI12" "OBV"       "JPYUSD"    "MarCap"    "MonthVol" 
[51] "MonthVal"  "GV"

Now, you just have to apply dput() to the above object to include the c():

dput(attr(terms.formula(formula), "term.labels"))
# output
c("m1", "m2", "m2m1", "topix", "sp500", "Open", "High", "Low", 
"Close", "Volume", "psyco12", "psyco24", "closeB5", "closeB10", 
"closeB20", "closeB60", "William5", "William8", "William13", 
"William21", "William34", "RSV9", "K", "D", "KD", "MA5", "MA10", 
"MA20", "MA60", "MA120", "MA240", "MA5MA20", "DI", "EMA12", "EMA26", 
"DIF", "MACD", "OSC", "change", "RSI6p", "RSI6m", "RSI6", "RSI12p", 
"RSI12m", "RSI12", "RSI6RSI12", "OBV", "JPYUSD", "MarCap", "MonthVol", 
"MonthVal", "GV")

Definition of formula:

formula <- as.formula(paste("label ~ ", paste(v1, collapse = " + "), sep = ""))
Related