I am experiencing a situation where I have a code
Split_data %>%
filter(data_availability_6m == 1) %>%
select(Total_cost_m_6m, Total_cost_ou_6m, Total_cost_in_6m) %>%
mutate(Total_cost = (Total_cost_m_6m + Total_cost_ou_6m + Total_cost_in_6m))
The "6m" at the end of the variable represents 6 months. Likewise, I want to run this code for 1yr, 18m, and 2yr. Instead of running the code again and again, I want to create a function to overcome this repeated coding. How can I write a function on this?
I tried creating a function:
Cost_summary <- function(timeX)
{
Split_data %>%
filter(data_availability_{{ timeX }} == 1) %>%
select(Total_cost_m_{{timeX}}, Total_cost_ou_{{timeX}}, Total_cost_in_{{timeX}}) %>%
mutate(Total_cost = (Total_cost_m_{{timeX}} + Total_cost_ou_{{timeX}} +
Total_cost_in_{{timeX}}))
But when I run the function, I get the error as
Error: unexpected '{' in:
" Split_data %>%
filter(data_availability_{"
I know that we need "{{}}" brackets in functions when using tidyverse application. Not sure what I am doing wrong.
Thanks in advance for the help.