【发布时间】:2022-06-22 21:24:05
【问题描述】:
我有一个文件夹,其中包含多个需要合并在一起的 CSV 文件,一次完成一个可能需要很长时间。 文件的性质是一个变量可能出现在 2 个或多个 CSV 文件中,但并非全部出现。但是,没有它的可能在其他地方有共同的变量。
下面我提供了一个说明性示例来说明这一点:-
#these files lie within the same folder on my machine
testdataframe_1<-data.frame(Column_1=c(1,2,3),
Column_2=c(4,5,6),
Column_3=c(7,8,9))
write.csv(testdataframe_1, file="testdataframe_1.csv")
testdataframe_2<-data.frame(Column_1=c(1,2,3),
Column_4=c(10,11,12),
Column_5=c(13,14,15))
write.csv(testdataframe_2, file="testdataframe_2.csv")
testdataframe_3<-data.frame(Column_6=c(16,17,18),
Column_7=c(19,20,21),
Column_4=c(10,11,12))
write.csv(testdataframe_3, file="testdataframe_3.csv")
testdataframe_4<-data.frame(Column_9=c(22,23,24),
Column_10=c(25,26,27),
Column_6=c(16,17,18))
write.csv(testdataframe_4, file="testdataframe_4.csv")
如您所见,Column_1 出现在 testdataframe_1 和 testdataframe_2 中,但没有出现在其余的其他数据框中。但是testdataframe_2和testdataframe_3的column_4,testdataframe_3和testdataframe_4的column_6。
有没有办法将这些 CSV 文件读入 R 并根据上述方式将它们合并到公共变量上,尤其是以自动化方式?
非常感谢!
【问题讨论】: