Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process formula object to a character object?

Tags:

string

r

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?

like image 662
recon Avatar asked Dec 31 '22 21:12

recon


1 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")
like image 155
akrun Avatar answered Jan 08 '23 02:01

akrun