Search code examples
rr-xlsx

how to merge columns of multiple excel files in R?


I have many Excel files.Every file having same format and number of columns.Suppose, file A containing data

    501 0006232.txt 0006235.txt 502 0.019047619 0.045989773
                    0006236.txt 503 0.042735043 0.116345945
                    0006239.txt 504 0.038095238 0.059372542
                    0006240.txt 505 0.0078125   0.007727828
                    0006247.txt 507 0.051724138 0.082823013

similarly, file B containing data

    502 0006235.txt 0006236.txt 503 0.075757576 0.377716498
                    0006239.txt 504 0.01754386  0.043033148
                    0006240.txt 505 0.012987013 0.014002801
                    0006246.txt 506 0.044444444 0.150648715
                    0006247.txt 507 0.044117647 0.105052539

Now, I want to merge these files in following way

501 0006232.txt 0006235.txt 502 0.019047619 0.045989773
                0006236.txt 503 0.042735043 0.116345945
                0006239.txt 504 0.038095238 0.059372542
                0006240.txt 505 0.0078125   0.007727828
                0006247.txt 507 0.051724138 0.082823013
502 0006235.txt 0006236.txt 503 0.075757576 0.377716498
                0006239.txt 504 0.01754386  0.043033148
                0006240.txt 505 0.012987013 0.014002801
                0006246.txt 506 0.044444444 0.150648715
                0006247.txt 507 0.044117647 0.105052539

But unable to do this. How can i do this ?


Solution

  • First I read first files in the directory. then combine other files one by one with it.Here is my code.

    f1=read.xlsx(list.files()[1],sheet = 1,colNames = FALSE)
    for(i in 2:length(list.files()))
    {
      f=read.xlsx(list.files()[i],sheet = 1,colNames = FALSE)
      f1=rbind(f1,f)
    }
    write.xlsx(f1,"titleResults.xlsx",)