I am trying to create subsets of 3 original dataframes (data_A, data_B, data_c) based on the value of a certain variable that is shared across those datasets (i.e. workhours). The value of the variable under which I want to create subsets will be the same across datasets. I want the created subsets to be labeled as Dataset_1 to Dataset_11 for subsets of data_A, Dataset_12 to Dataset_22 for subsets of data_B, and Dataset_23 to Dataset_33 for subsets of data_C.
Right now I have the following solution:
for (i in 1:11){
assign(paste0("Dataset_",i), subset(data_A, workhours>=(0+(i-1)*5)))
}
for (i in 12:22){
assign(paste0("Dataset_",i), subset(data_B, workhours>=(0+(i-12)*5)))
}
for (i in 23:33){
assign(paste0("Dataset_",i), subset(data_C, workhours>=(0+(i-23)*5)))
}
This works fine. However, is it possible to use merely 1 loop as opposed to 3?
EDIT:
solution:
for (i in 1:11){
assign(paste0("Dataset_",i), subset(data_A, workhours>=((i-11)*5)))
assign(paste0("Dataset_",i+11), subset(data_B, workhours>=((i)*5)))
assign(paste0("Dataset_",i+23), subset(data_C, workhours>=((i)*5)))
}
another solution in can be found below
0+in all loops doesn't do anything. 2. Is it possible that in the first loop it should bei-1instead ofi-11?