Here you can split the column names into indx , get a subset of the data in the list using lapply and [ , set the names of the list items using setNames and use list2env if you need them as separate data sets (not recommended, since most operations can be performed in list, and later, if you want, it can be saved using write.table using lapply .
list2env( setNames( lapply(split(colnames(df), indx), function(x) df[x]), paste('df', sort(unique(indx)), sep="_")), envir=.GlobalEnv) head(df_1,2)
Or using Map . This is similar to the above approach, i.e. separate the column names by indx and use [ to extract the columns, and the rest as above.
list2env(setNames(Map(`[` , list(df), split(colnames(df), indx)), paste('df',unique(sort(indx)), sep="_")), envir=.GlobalEnv)
Update
You can do:
indx1 <- factor(indx, levels=unique(indx)) split(colnames(df), indx1)
source share