I have a list of dataframes:
set.seed(1)
S1 = data.frame(replicate(2,sample(0:130,30,rep=TRUE)))
S2 = data.frame(replicate(2,sample(0:130,34,rep=TRUE)))
S3 = data.frame(replicate(2,sample(0:130,21,rep=TRUE)))
S4 = data.frame(replicate(2,sample(0:130,26,rep=TRUE)))
df_list1 = list(S1 = S1, S2 = S2, S3 = S3, S4 = S4)
set.seed(2)
S1 = data.frame(replicate(2,sample(0:130,30,rep=TRUE)))
S2 = data.frame(replicate(2,sample(0:130,34,rep=TRUE)))
S3 = data.frame(replicate(2,sample(0:130,21,rep=TRUE)))
S4 = data.frame(replicate(2,sample(0:130,26,rep=TRUE)))
df_list2 = list(S1 = S1, S2 = S2, S3 = S3, S4 = S4)
set.seed(3)
S1 = data.frame(replicate(2,sample(0:130,30,rep=TRUE)))
S2 = data.frame(replicate(2,sample(0:130,34,rep=TRUE)))
S3 = data.frame(replicate(2,sample(0:130,21,rep=TRUE)))
S4 = data.frame(replicate(2,sample(0:130,26,rep=TRUE)))
df_list3 = list(S1 = S1, S2 = S2, S3 = S3, S4 = S4)
set.seed(4)
S1 = data.frame(replicate(2,sample(0:130,30,rep=TRUE)))
S2 = data.frame(replicate(2,sample(0:130,34,rep=TRUE)))
S3 = data.frame(replicate(2,sample(0:130,21,rep=TRUE)))
S4 = data.frame(replicate(2,sample(0:130,26,rep=TRUE)))
df_list4 = list(S1 = S1, S2 = S2, S3 = S3, S4 = S4)
df_list = list (df_list1, df_list2, df_list3, df_list4)
names(df_list) = c("AB_df", "BC_df", "DE_df", "FG_df")
df_list = unlist(df_list, recursive = FALSE)
Now I want to rearrange the order of the dataframes within the list. The pattern should be 1,5,9,13, 2,6,10,14,3,7,11,15,4,8,12,16
I programmed it like this:
subs = 4 # Need the subs, since I have different numbers for this
idx = seq (1, length(df_list), subs)
nameorder = c(names(df_list[idx]), names(df_list[idx+1]),
names(df_list[idx+2]),names(df_list[idx+3]))
My problem is now, how can I use the nameorder
list to change my df_list (names should be kept). I guess its pretty simple, but I just cant get it.
You can try a simple command df_list
as below to reorder list of data frames
:
df_list[nameorder]