我有大量数据框要合并。每个都有数百列。我想在执行此操作之前识别所有不匹配的列名。到目前为止,我可以生成一个不匹配列表,但是格式很糟糕,我不太清楚如何判断它们来自哪个数据框。
#create data
df1 <- data.frame("col1" = 3:4, "Age" = c(22,16), "Name" = c("James","Jim"))
df2 <- data.frame("col1" = 3:4, "Age" = c(18,19), "Name" = c("Mike","Mia"))
df3 <- data.frame("mismatch_col_name_1" = 1:2, "Age" = c(21,15), "name" = c("John","Dora"))
df4 <- data.frame("mismatch_col_name_2" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))
files <- list(df1, df2, df3, df4)
# find mismatched column names
mismatches <- NULL
for (i in 1:(length(files) - 1)) {
mismatches <- c(mismatches, setdiff(colnames(files[[i]]), colnames(files[[i+1]])))
}
mismatches <- c(mismatches, setdiff(colnames(files[[length(files)]]), colnames(files[[1]])))
print(mismatches)
[1] "col1" "Name" "mismatch_col_name_1" "name"
[5] "mismatch_col_name_2"
所需的输出将类似于:
“df3” “mismatch_col_name_1” “名称”
"df4" "mismatch_col_name_2" "名称"
甚至 df 名称和列号。对任何解决方案或更好的方法感兴趣。