We could split
the dataset (‘df’) with ‘1416’ columns to equal size ‘118’ columns by creating a grouping index with gl
lst <- setNames(lapply(split(1:ncol(df), as.numeric(gl(ncol(df), 118,
ncol(df)))), function(i) df[,i]), paste0('site', 1:12))
Or you can create the ‘lst’ without using the split
lst <- setNames(lapply(seq(1, ncol(df), by = 118),
function(i) df[i:(i+117)]), paste0('site', 1:12))
If we need to create 12 dataset objects in the global environment, list2env
is an option (I would prefer to work within the ‘lst’ itself)
list2env(lst, envir=.GlobalEnv)
Using a small dataset (‘df1’) with ‘8’ columns
lst1 <- setNames(lapply(split(1:ncol(df1), as.numeric(gl(ncol(df1),
2, ncol(df1)))), function(i) df1[,i]), paste0('site', 1:4))
list2env(lst1, envir=.GlobalEnv)
head(site1,3)
# V1 V2
#1 6 12
#2 4 7
#3 14 14
head(site4,3)
# V7 V8
#1 10 2
#2 5 4
#3 5 0
data
set.seed(24)
df1 <- as.data.frame(matrix(sample(0:20, 8*10, replace=TRUE), ncol=8))
3
solved Selecting column sequences and creating variables