R:eval(parse(...))通常不是最佳的 [英] R: eval(parse(...)) is often suboptimal

查看:179
本文介绍了R:eval(parse(...))通常不是最佳的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

require('fortunes')
fortune('106')
Personally I have never regretted trying not to underestimate my own future stupidity.
   -- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering a question triggered
      by the infamous fortune(106))
      R-help (January 2007)

因此,如果eval(parse(...))次优,那么还有另一种方法可以做到这一点?

So if eval(parse(...)) is suboptimal what is another way to do accomplish this?

我正在使用RCurl从网站上调用一些数据,在rjson包中使用fromJSON()后得到的是列表中的列表.列表的一部分具有订单号的名称,该订单号将根据订单而改变.该列表如下所示:

I am calling some data from a website using RCurl, what i get after using fromJSON() in the rjson package is a list within a list. Part of the list has the name of an order number that will change depending on the order. The list looks something like:

$orders
$orders$'5810584'
$orders$'5810584'$quantity
[1] 10

$orders$'5810584'$price
[1] 15848

我想提取$orders$'5810584'$price

说列表在对象dat中.我使用eval(parse(...))提取的内容是:

Say the list is in the object dat. What I did to extract this using eval(parse(...)) was:

or_ID <- names(dat$orders) # get the order ID number
or_ID
"5810584"
sell_price <- eval(parse(text=paste('dat$',"orders$","'", or_ID, "'", "$price", sep="")))
sell_price
15848

做这件事的最佳方法是什么?

What would be a more optimal way of doing this?

推荐答案

实际上,该列表可能看起来有些不同. "$"约定有些误导.试试这个:

Actually the list probably looks a bit different. The '$' convention is somewhat misleading. Try this:

dat[["orders"]][[ or_ID ]][["price"]]

'$'不评估其参数,但是"[["评估,因此or_ID将变成"5810584".

The '$' does not evaluate its arguments, but "[[" does, so or_ID will get turned into "5810584".

这篇关于R:eval(parse(...))通常不是最佳的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆