With merging several txt files into one

Asked

Viewed 1,515 times

3

I have 60 txt files all with the same number of columns. The files have no header (column name).

How can I join them all in one file just below each other in R.

  • You can do it by adapting a little what is here: https://answall.com/a/48821/6036

  • You can also use this @Athospd https://github.com/Athospd/forkliftrpackage

1 answer

4


The code below should be adapted to your specific needs, but it should give a good starting point. Heed: R must be configured for the directory in which the . txt files are located:

lista    <- list.files(pattern="txt")
arquivos <- lapply(lista, function(x) read.table(x, header=FALSE, sep=" ")) 
dados    <- do.call("rbind", arquivos) 

Let’s break him in pieces:

  • list.files returns all files from the directory according to a pattern. In this case, all that have txt in the title

  • lapply apply the function read.table for each file name present in lista, whereas the files have no header and the column separator is a blank space. In your case, it may be that this separator is a comma, a semicolon or tab marks. Adjust the code according to your needs.

  • do.call apply the function rbind, responsible for joining data frames according to one below the other, in the data that were stored on arquivos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.