r - Function for querying a data.table -
i have data.table called prices has 3 columns: ticker, date , price. have run
setkey(prices,ticker,date) if code, works
prices[list("msft",as.date("2013-01-15")] returning row msft in 2013-01-15 however, if write function
getprice <- function(ticker,date) { prices[list(ticker,date)] } it returns whole data.table suspect has scoping in parameter, can't work. how query data.table if don't know parameters in advance?
your problem variable names in function. change them e.g. x , y (so not same column names in data.table) , work. you're doing constructing data.table of ticker , date columns , joining that, recovering original data.
another (more robust) option smth in function:
getprice <- function(ticker,date) { tmp = list(ticker, date) prices[tmp] } see faqs 2.12 , 2.13 more on - http://datatable.r-forge.r-project.org/datatable-faq.pdf
Comments
Post a Comment