简体   繁体   中英

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. Ex: Data for Alabama July 2021 would be followed be data for Alabama July 2022. Thank you!

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 .

To combine these datasets in r, I would use cbind(file1,file2). Personally, I would transpose the dataframe so each row is a trip rather than each column. This is what it will look like using the 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")),])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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