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(...))
是次優的什麼是另一種方法做到這一點?R:eval(parse(...))往往不是最理想的
我打電話給一個網站使用RCurl的一些數據,我在rjson軟件包中使用fromJSON()
後得到的是列表中的一個列表。部分清單中的訂單號碼的名稱將根據訂單而變化。該列表看起來是這樣的:
$orders
$orders$'5810584'
$orders$'5810584'$quantity
[1] 10
$orders$'5810584'$price
[1] 15848
我想提取$orders$'5810584'$price
值說出列表中的對象dat
。我做了提取這種使用eval(parse(...))
是:
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
什麼是這樣做的更好的方法?
dat $ orders [[or_ID]] $ price? – Dason
不起作用,因爲or_ID是一個字符而不是數字。 使用,dat $ orders [[1]] $ price可以工作 – Kevin
使用'match'來獲取名稱在'names(dat $ orders)'中的位置。 – joran