What I need to do is to do is to create new variables ("new_var" in my code) using the ave function
https://www.rdocumentation.org/packages/stats/versions/3.6.0/topics/ave
by changing each time the grouping variable ("new_year" in my code) according to the above mentioned documentation
I have tried replacing the variable new_year with colnames(dat)[colnames(dat)==new_year] and with dat[new_year] but the code didn't work
for (year in 1990:2015){
new_var <- paste("active_year_calc", year , sep="_")
new_year <- paste("active_year", year , sep="_")
dat <- within(dat, {
new_var<-ave(sum_variable, company_name,new_year, FUN = length
})
}
I expect to create new columns in my data set with the names :
active_year_calc_1990, active_year_calc_1991,active_year_calc_1992,......active_year_calc_2015
according to the outputs of ave function in which the new year changes as following new_year_1990,new_year_1991,new_year_1992,.....