I'm trying to workout some code mixing pipe operators and a function.
The following line of code should compute the daily returns for some stocks that have been previously group by name.
mod_stock_data$daily_return = mod_stock_data %>% group_by(gvkey, iid) %>% dailyReturn(prccd)
mod_stock_data is a dataframe that contains gvkey (stock's key name), iid (identfier), and prccd (closing value).
I'm using a pipe operator for the first step of grouping the dataframe by the stock's name. Once this operation is performed, the result of the group by is piped towards dailyReturn() function.
But instead of adding a new column with the daily returns, I get the following error:
Error in dailyReturn(., prccd) : unused argument (prccd)
Anyone can help?
The dailyReturn function:
dailyReturn <- function(tx) {
returns = tx / lag(tx, 1) - 1
return(returns)
}