简体   繁体   English

根据Row信息合并两个CSV文件

[英]Merge two CSV files based on Row information

I have two CSV files, each with rows: r1 (Origin), r2: Destination, r3: Direction, r5( Year) r6(month) etc etc. The difference between the two files is the years, with file 1 containing observations till July 2021, and file two with observations from that to November 2022. Is there anyway I could merge the two files together by letting R know where to paste the data in based on the information from the previous column.我有两个 CSV 文件,每个文件都有行:r1(原点)、r2:目的地、r3:方向、r5(年)r6(月)等。两个文件之间的区别是年份,文件 1 包含直到2021 年 7 月,并提交两个文件,其中包含从那时到 2022 年 11 月的观察结果。无论如何,我是否可以根据上一栏中的信息让 R 知道将数据粘贴到何处,从而将这两个文件合并在一起。 Ex: Data for Alabama July 2021 would be followed be data for Alabama July 2022. Thank you!例如:阿拉巴马州 2021 年 7 月的数据之后是阿拉巴马州 2022 年 7 月的数据。谢谢!

I tried to manually do it by pasting in the relevant data into the respective fields but it is extremely time consuming.我尝试通过将相关数据粘贴到相应字段中来手动完成此操作,但这非常耗时。

First of all I am assuming your data looks something like this: file1 and file2 .首先,我假设您的数据看起来像这样: file1file2

To combine these datasets in r, I would use cbind(file1,file2).要在 r 中组合这些数据集,我会使用 cbind(file1,file2)。 Personally, I would transpose the dataframe so each row is a trip rather than each column.就个人而言,我会转置 dataframe,这样每一行都是一次旅行,而不是每一列。 This is what it will look like using the t() function: final form .这是使用 t() function: final form时的样子。

file1 <- read_excel("file1.xlsx", col_names = FALSE)

file2 <- read_excel("file2.xlsx", col_names = FALSE)

file3 <- cbind(file1,file2)

finalform<- as.data.frame(t(file3))

finalform <- finalform[order(as.Date(finalform$V4, format="%Y")),]

finalform <- as.data.frame(finalform[order(as.Date(finalform$V5, format="%m")),])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM