add multiple columns to multiple data table frames at the same time

You are on the right track. You need to get all the data in a list and iterate over it through lapply. However, I don’t think the operations that you want to perform need to be grouped by month, year or week. You can perform them directly on each data table.

library(data.table)

list_data <- mget(paste0('dt', 1:3))
list_data <- lapply(list_data, function(x) {
  x[,   c('year', 'year_num', 'months', 'months_num', 'days', 'weekday') := 
        list(as.factor(year(EndDate)), year(EndDate), as.factor(month(EndDate)), 
             month(EndDate), x, weekdays(EndDate))] 
})

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top