I want to create a list or many data.frame from a dataframe at the same time after splitting a matrix. I am using the function combn to create a matrix. For instance:
combos<-combn(1:3, 2)
combos
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 2 3 3
After I have a data frame with 3 columns.
col1<-c(0,2,4);col2<-c(1,3,5);col3<-c(6,7,8)
df<-cbind.data.frame(col1,col2,col3)
df
col1 col2 col3
1 0 1 6
2 2 3 7
3 4 5 8
Using combos I would like to get this results in data frame or list:
df1
col1 col2
1 0 1
2 2 3
3 4 5
df2
col1 col3
1 0 6
2 2 7
3 4 8
df3
col2 col3
1 1 6
2 3 7
3 5 8
After of that, I would want to join these dataframe or list with other dataframes or list to get this result: using this new data dfo
col1<-c('a','c'); col2<-c('b','d')
dfo<-cbind.data.frame(col1,col2)
col1 col2
1 a b
2 c d
df1o
col1 col2
1 0 1
2 2 3
3 4 5
4 a b
5 c d
df2o
col1 col3
1 0 6
2 2 7
3 4 8
4 a b
5 c d
df3o
col2 col3
1 1 6
2 3 7
3 5 8
4 a b
5 c d
I have 3000 df and 5000 dfo
Actually, you can use lapply
to loop over a list.
combos <- combn(1:3, 2)
col1 <- c(0,2,4); col2 <- c(1,3,5); col3 <- c(6,7,8)
df <- cbind.data.frame(col1,col2,col3)
df.1 <- lapply(1:ncol(combos), function(i){df[, combos[,i]]})
col1 <- c('a','c'); col2 <- c('b','d')
dfo <- cbind.data.frame(col1,col2)
dfo.1 <- lapply(df.1, function(x){
names(dfo) <- names(x)
return(rbind(x, dfo))
})
# [[1]]
# col1 col2
# 1 0 1
# 2 2 3
# 3 4 5
# 4 a b
# 5 c d
#
# [[2]]
# col1 col3
# 1 0 6
# 2 2 7
# 3 4 8
# 4 a b
# 5 c d
#
# [[3]]
# col2 col3
# 1 1 6
# 2 3 7
# 3 5 8
# 4 a b
# 5 c d