Help with functions and loops

Are there any way to add these new columns for the different ''leads ''using a loop?

profitlead <- function(df,col_name,col1,col2,n){
df[[col_name]] <- (lead(df[[col1]],n)-df[[col1]])*(1000000/df[[col1]])

cumsumlead <- function(df,col_name,col2){
df[[col_name]] <- (cumsum(df[[col2]]))

#Filter for adding decired startdate
BTC_2 <- BTC_NOK_hist %>% select(Date,close) %>%
filter(Date >= as.Date("2021-03-03"))
#making a new dataset with different lead
BTC_2 <- profitlead(BTC_2,"Profitlead5","close",n=5)
BTC_2 <- profitlead(BTC_2,"Profitlead10","close",n=10)
BTC_2 <- profitlead(BTC_2,"Profitlead40","close",n=40)
BTC_2 <- profitlead(BTC_2,"Profitlead60","close",n=60)

BTC_2 <- cumsumlead(BTC_2,'cumsumlead5','Profitlead5')
BTC_2 <- cumsumlead(BTC_2,'cumsumlead10','Profitlead10')
BTC_2 <- cumsumlead(BTC_2,'cumsumlead40','Profitlead40')
BTC_2 <- cumsumlead(BTC_2,'cumsumlead60','Profitlead60')

Can you provide a reproducible example?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.