I have this following problem:
If I have multiple data frames stored inside a list and I would like to shift the last 2 columns to a position in between the 1st and 2nd column. The index of the 1st and 2nd in all the data frames is always the same but the index of the last 2 columns is always different.
I give and example for two data frames:
df1
c1. c2 c3 c4 c5 c6
1. 4. a. d. 7. 10
2. 5. b. e. 8. 11
3. 6. c. f. 9. 12
Output
df1
c1. c5 c6 c2 c3 c4
1. 7. 10 4. a. d.
2. 8. 11 5. b. e.
3. 9. 12 6. c. f.
df2
c1. c2 c2_1 c3 c3_1 c4 c5 c6
1. 4. a1. b5. d. h 7. 10
2. 5. b. 7g e. j 8. 11
3. 6. c. 9r f. l 9. 12
Output:
c1. c5 c6 c2 c2_1 c3 c3_1 c4
1. 7. 10 4. a1. b5. d. h
2. 8. 11 5. b. 7g e. j
3. 9. 12 6. c. 9r f. l
df1 and df2 are stored in list_df
list_df<-list(df1,df2)
I wrote something like this but it does not seem to work:
list_new<-list()
for (i in 1:length(list_df)){
list_new[[i]]<- function(x) {cbind(x[[i]][,1], x[[i]][,ncol(x[[i]]-2)], x[[i]][,2:ncol(x[[i]])])
}}