-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbind_list_dfs_no_id.R
137 lines (81 loc) · 3.49 KB
/
rbind_list_dfs_no_id.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
##########################################################
#### Description #####
##########################################################
# A function to rbind list of dataframes
# with no equal columns
# and with no common id value
##########################################################
#### Dependencies #####
##########################################################
# Depends on Base R only
##########################################################
#### Example DF #####
##########################################################
# Example df
ex_df <- cbind(c( seq(1, 10, 1), rep("NA", 0), seq(1,10, 1) ),
c( seq(1, 7, 1), rep("NA", 3), seq(1, 12, 1) ),
c( seq(1, 3, 1), rep("NA", 7), seq(1, 5, 1), rep("NA", 5) ))
# Making colnames and rownames
colnames(ex_df) <- 1:dim(ex_df)[2]
rownames(ex_df) <- 1:dim(ex_df)[1]
# Making an unequal list of dfs,
# without a common id column
list_df <- apply(ex_df=="NA", 2, ( table) )
##########################################################
#### Arguments of the Function #####
##########################################################
# A list of dataframes
##########################################################
#### The function #####
##########################################################
# The function to rbind it
rbind_null_df_lists <- function ( list_of_dfs ) {
# check code
# tables_ln
# list_of_dfs <- list_df
if ( length(names(list_of_dfs)) == 0 ) {
names(list_of_dfs) <- 1:length(list_of_dfs )
length_df <- do.call(rbind, (lapply( list_of_dfs, function(x) length(x))))
max_no <- max(length_df[,1])
if ( length(names(length_df)) == 0 ) {
names_list<- ""
}
if ( length(names(length_df)) != 0 ) {
max_df <- length_df[max(length_df),]
name_df <- names(length_df[length_df== max_no][1])
names_list <- names(list_of_dfs[ name_df][[1]])
}
}
if ( length(names(list_of_dfs)) != 0 ) {
length_df <- do.call(rbind, (lapply( list_of_dfs, function(x) length(x))))
max_no <- max(length_df[,1])
if ( length(names(length_df)) == 0 ) {
names_list<- ""
}
if ( length(names(length_df)) != 0 ) {
max_df <- length_df[max(length_df),]
name_df <- names(length_df[length_df== max_no][1])
names_list <- names(list_of_dfs[ name_df][[1]])
}
}
df_dfs <- list()
for (i in 1:max_no ) {
df_dfs[[i]] <- do.call(rbind, lapply(1:length(list_of_dfs), function(x) list_of_dfs[[x]][i]))
}
df_cbind <- do.call( cbind, df_dfs )
if ( length(rownames( df_cbind ) ) != length(rownames (length_df) )) {
rownames (length_df) <- rownames (length_df)
}
if ( length(rownames( df_cbind ) ) == length(rownames (length_df) )) {
rownames( df_cbind ) <- rownames (length_df)
}
if( names_list != "" ) {
colnames( df_cbind ) <- names_list
}
df_cbind
}
##########################################################
#### Running the example #####
##########################################################
# Uncomment the below line in order to run the example
# rbind_null_df_lists ( list_of_df )