Creating expression tree in R

The substitute function in `R` creates a language object in the form of a tree that one can parse. How can I create the tree from scratch using list or else to then give it to eval? # substitute gives a tree representation of the expression a=1; b=2; e1 = substitute(a+2*b) eval(e1) #gives 5 as expected e1 # is type language e1[[1]] # this is `+` e1[[2]] # this is 'a' type symbol e1[[3]] # this is type language e1[[3]][[1]] # this is `*` etc.... I would like to know how I can reconstruct the `e1` object programmatically. Ideally I create an object of intricated lists with the correct object in them and maybe I call some `as.language` on the `list` object. However that does not work. For instance: # how to construct the tree? eval(list(as.symbol('+'),1,1)) # does not return 2 eval(as.expression(list(as.symbol('+'),1,1))) # does not return 2 One way is to just generate the string '1+1' and then parse it, but it does not seem elegant to generate strings to parse them again when you have the tree in the first place! eval(parse(text='1+1')) # does return 1, but not elegant if tree is # large and already in memory Thanks for your help!

以上就是Creating expression tree in R的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏